is there a way to pass data directly in Step definition without passing in feature file? - selenium

i want to use dataprovider to pass data directly into step definition without passing from feature file, as i want to pass null values as well. here is what i am doing.
Scenario: User should get empty field highlight, when that fields is empty and clicked submit. When Submit is clicked after providing values in nethier or either of Reg Id or PC
#Test(dataProvider = "getData")
#When("^Submit is clicked after providing values in nethier or either of Reg Id or PC$")
public void submit_is_clicked_after_providing_values_in_nethier_or_either_of_reg_id_something_or_pc_something(
String regvalue, String pcvalue) throws Throwable {
//code
}
#DataProvider
public Object[][] getData() {
Object[][] data = new Object[3][2]; // 3 is number of combinations and 2 is number of values
// 1st set
data[0][0] = "Username1";
data[0][1] = null;
// 2nd set
data[1][0] = null;
data[1][1] = "Password1";
// 3nd set
data[2][0] = null;
data[2][1] = null;
return data;
}
Error i am getting is
Step [^Submit is clicked after providing values in nethier or either of Reg Id or PC$] is defined with 2 parameters at 'com.commcard.stepdefinition.StepDef.submit_is_clicked_after_providing_values_in_nethier_or_either_of_reg_id_something_or_pc_something(String,String) in file:/D:/Eclipse-Workspace/CucumberProject.CitiCommCard/target/test-classes/'.
However, the gherkin step has 0 arguments.

You can use a yml file as a data-lookup. For JSON style testing I would advocate this. As you can use a regular fixture or build it up mid-process.
So you could have something like this.
Given I have a valid request to create a user
But the username is invalid
Given I have a valid request to create a user
But the username is too short
# yaml file
user:
create:
issues:
username:
invalid: "Can't Use W3!rd char$"
too_short: "usrnm"
Then your steps just use whatever programming language you use and convert the yml into a data lookup (Hash/List), and alter the keys.

Related

Recieving user input with slash command Discord.Net

Is there a way to receive user inputted data using the current format I have, I have seen that there is a way to do it using the slashCommandBuilder as seen here discord.net how to get user input data on slash command option? , an example of my slash command is
[SlashCommand("ping", "ping pong")]
public async Task HandlePing()
{
await RespondAsync("pong!");
}
I'd like to be able to use user input eg: user inputs /ping dog and it prints dog
there was another post like this; use the following code:
public async Task HandlePing(SocketSlashCommand command)
{
var userInput = command.Data.Options.First().Value.ToString();
await command.RespondAsync("pong!");
}
If your input is IUser, or an int, remove ToString() at the end of the variable, userInput

How to pass multiple values using same parameter in TestNG

The website I am working on has many textboxes, after filling all the boxes I have to click on Add and repeat the same filling again on the same screen. How can I Pass the different sets of values without closing the browser using TestNG.
set 1:
Name = harry
jobid =123
occumpation = PA
Click on add button and on the same page repeat the below
Set 2:
Name = john
jobid = 125
occumpation = PA
Use TestNG data provider.
#Test(dataProvider="webSiteData")
public void myTestCase(String name, String jobId, String occupation)
{
//Your test code goes here...
}
#DataProvider(name="webSiteData")
public Object[][] getData()
{
Object [][] myData = {{"harry","123","PA"},
{"john","125","PA"}};
return myData;
}
Note: Here each row of myData represents the number of times this test method will be executed. The column represents the parameters to pass to test method. So two rows mean your test method will be executed two times with two different sets of parameters.
You can use TestNG Data provider,
Reference: http://toolsqa.com/selenium-webdriver/testng-parameters-data-provider/
OR https://www.guru99.com/parameterization-using-xml-and-dataproviders-selenium.html

Does this saving/loading pattern have a name?

There's a variable persistence concept I have integrated multiple times:
// Standard initialiation
boolean save = true;
Map<String, Object> dataHolder;
// variables to persist
int number = 10;
String text = "I'm saved";
// Use the variables in various ways in the project
void useVariables() { ... number ... text ...}
// Function to save the variables into a datastructure and for example write them to a file
public Map<String, Object> getVariables()
{
Map<String, Object> data = new LinkedHashMap<String, Object>();
persist(data);
return(data);
}
// Function to load the variables from the datastructure
public void setVariables(Map<String, Object> data)
{
persist(data);
}
void persist(Map<String, Object> data)
{
// If the given datastructure is empty, it means data should be saved
save = (data.isEmpty());
dataHolder = data;
number = handleVariable("theNumber", number);
text = handleVariable("theText", text);
...
}
private Object handleVariable(String name, Object value)
{
// If currently saving
if(save)
dataHolder.put(name, value); // Just add to the datastructure
else // If currently writing
return(dataHolder.get(name)); // Read and return from the datastruct
return(value); // Return the given variable (no change)
}
The main benefit of this principle is that you only have a single script where you have to mention new variables you add during the development and it's one simple line per variable.
Of course you can move the handleVariable() function to a different class which also contains the "save" and "dataHolder" variables so they wont be in the main application.
Additionally you could pass meta-information, etc. for each variable required for persisting the datastructure to a file or similar by saving a custom class which contains this information plus the variable instead of the object itself.
Performance could be improved by keeping track of the order (in another datastructure when first time running through the persist() function) and using a "dataHolder" based on an array instead of a search-based map (-> use an index instead of a name-string).
However, for the first time, I have to document this and so I wondered whether this function-reuse principle has a name.
Does someone recognize this idea?
Thank you very much!

Cakephp Validation on API input data

I have an API that receives data in JSON, XML and FORM data. The API needs to do a bit of validation that depending on the data provided, depends on the data to be validated. I have a "sendmode" field that is required for each API call. If the sendmode = 1 then mobile,firstname and lastname are required fields, however, if sendmode is 2 then group is a required field.
My question is, how can I validate my data in my model according to data provided in the API call without having to do a bunch of If statements at the beginning of my controller?
Thanks
public function api_send() {
//Get Params
$data = $this->request->input('json_decode', TRUE);
//validate here -> first required
$data['sendmode'] //need to validate
$data['subject'] //need to validate
$data['message'] //need to validate
if($validate == false){
return;
break;
}
switch($data['sendmode']){
case 1:
$data['mobile'] //need to validate
$data['firstname'] //need to validate
$data['lastname'] //need to validate
if($validate == TRUE){
//save data
}
//etc
}
}
So I want all my validation done in my model, but first, I need to validate 3 fields, if thats true I need to continue, then if sendmode is 1, I need to validate other fields, as these fields will not exist if sendmode is 2 or 4 etc
First you pass that json (?) data to a model method. Inside your model have a method that switches the validation rules based on the type.
// Controller
$this->Model->validateApiCall($this->request->input('json_decode', TRUE));
// Model
public function setValidationRules($type = 'someDefault') {
switch($type){
case SendMode::GROUP_FIELDS:
$this->validate = array(/* your rules for that type here */);
// ...
}
public function validateApiCall($data) {
$this->setValidationRules($data['type']);
// return or do something else with the data depending on it if its validated or not
return $this->validate($data);
}
Oh, and if you really want to use integers for the types I would recommend to use constants like SendMode::GROUP_FIELDS. I usually create a tableless model for that. Constants are a lot better because if you do a typo it brings up a syntax error and while using plain integers nobody never ever is going to remember what the meaning of a random 3 or 4 or 1 is.

Session variables values NOT available accross web pages in WebMatrix

I would like to use session variables throughout my WebMatrix web pages.
For an unknown reason, they are not available accross the pages (only to the page where they are defined).
In my login page, code section:
if (WebSecurity.Login(userName, password, rememberMe)) {
// Session variable holding the UserType
var db = Database.Open("MyDatabase");
var userTypeData = db.QuerySingle("SELECT Name FROM UserTypes INNER JOIN UserProfiles ON UserProfiles.UserTypeId = UserTypes.UserTypeId WHERE UserId = #0",WebSecurity.CurrentUserId);
Session ["UserType"] = userTypeData.Name;
// Eventual redirection to the previous URL
var returnUrl = Request.QueryString["ReturnUrl"];
if (returnUrl.IsEmpty()) {
Response.Redirect("~/auth/authenticated");
}
else {
Context.RedirectLocal(returnUrl);
}
I get here a "Cannot perform runtime binding on a null reference" hence if there is always a UserType. THis is the fist problem.
In the "authenticated" page where I'm redirected, if I use exactly the same query and session variable definition, I can display it as :
You are a user of type: #Session["UserType"] -> HTML section
On other pages, I'm trying to ´display or hidden (update) buttons` through the session variable.
#if (Session["UserType"]=="here the variable value; a string") {
<a class="linkbutton" href="condoedit?condoId=#condoData.CondoId">Update</a>
}
Button is never displayed as the session variable appears to be empty !!
Change your code to test if there are problems with the query (BTW, use QueryValue instead of QuerySingle if you need only the user name).
Try something like
var db = Database.Open("MyDatabase");
var userTypeData = db.QueryValue("SELECT Name FROM UserTypes INNER JOIN UserProfiles ON UserProfiles.UserTypeId = UserTypes.UserTypeId WHERE UserId = #0",WebSecurity.CurrentUserId);
if (String.IsNullOrEmpty(userTypeData)) {
Session["UserType"] = "IsEmpty";
} else {
Session["UserType"] = userTypeData;
}
and test on other pages if the session variable value is "IsEmpty".
Edit
In the other pages convert to string the session variable value and store it into a local variable
var uType = Session["UserType"].ToString();
When the item is stored in session, it is stored as an object data type, so it will need to be cast back to its original data type if there is no implicit cast between object and the original type. (mikesdotnetting.com)
-> Session["UserType"].ToString()
#if (Session["UserType"].ToString()=="here the variable value; a string") {
<a class="linkbutton" href="condoedit?condoId=#condoData.CondoId">Update</a>
}