loop a feature file on the given data - java

Background:
There are more than 500 users. Their user name and password are stored in a CSV file.
Each user must pass this .feature file which consists of 18 scenarios.
Problem:
I know we can use data tables, scenario outline, but data mentioned here will be limited to associated scenarios only not for entire feature file. I want once .feature done executing first row of data it should go to next row from CSV file, and continue doing it until the end of CSV file.
I tried replacing Data table with MS Excel sheet, used (POI apache files to read spreadsheet), but again same thing happened it ran for that particular scenario only.
E.g. This a .feature file
#regression #userValidation
Feature: User permission validation
Scenario Outline: Verify that user is able to login
Given I am on login page
When I enter "username" and "password"
Then I see new user successfully loggedin
Examples:
| username |password| // picked from excel sheet
| uname |pwd | // picked from excel sheet
Scenario: Verify that user can change the password
Given I am on user profile page
When I enter change password twice
Then I get password successfully changed pop-up
Now, scenario one keep on running 500 times (number of rows in csv file) once all data is finished, it starts second scenario. What I want is it both scenario
should run for row 1, then both scenario should for row 2 and so on for 500 times.
How can I do it? Is there a Java or Junit way to do it, if not possible in Cucumber?
I am using Java, Selenium WebDriver, JUnit, Cucumber, Maven on Windows

One of the way is adding examples in each scenario in the feature file, you are already reading data from external source so it will not duplicate data at scenario level. Still you need to add examples to each scenario. For example:
#regression #userValidation
Feature: User permission validation
Scenario Outline: Verify that user is able to login
.....
Examples:
| username |password| // picked from excel sheet
| uname |pwd | // picked from excel sheet
Scenario Outline: Verify that user can change the password
...
Examples:
| username |password| // picked from excel sheet
| uname |pwd | // picked from excel sheet
In this case you need to be cautious on execution order of each scenario. If you use gherkin with qaf, it may look like below:
#regression #userValidation
Feature: User permission validation
Scenario: Verify that user is able to login
.....
Examples: {'datafile':'resources/usersdata.csv'}
Scenario: Verify that user can change the password
...
Examples: {'datafile':'resources/usersdata.csv'}
More over, with latest BDD2 syntax, you can have your feature file as below:
#regression #userValidation
#datafile:resources/usersdata.csv
Feature: User permission validation
Scenario: Verify that user is able to login
.....
Scenario: Verify that user can change the password
...
Regarding execution order, qaf makes sure to run scenario in order they defined in feature file. Still you can specify priority in meta-data when using BDD2 syntax, for instance, #priority:1 on first scenario, #priority:2 on second and so forth.
Other alternate for above case is change first scenario as per-condition and consider it as background, for that refer how to use background with examples.

Related

How to avoid sendKeys method in selenium to clear previously filled text?

The android application that I'm testing using Appium with Java has the tag/mention users feature. Now I want to test this feature. I'd like to send multiple mentions and verify that the mentions are sent or not. The application that I'm testing is similar to slack/messaging application.
Test case steps:
Open chatroom
Tap on send message field to activate the keyboard
Type # and wait for list of users to tag/mention appear
Tap user 1
Send a space after the user1 is tagged/mentioned
Type # again and wait for list of users to tag/mention appear
Tap user 2
Tap message send button
I'm using page object model. So to perform the above steps I created a method named sendMultiMention in the page class as follows:
#Override
public ChatroomPage sendMultiMention(int index0, int index1){
sendKeyToElement(SENDMESSAGEFIELD, "#");
List<MobileElement> mentionPersonNamesList =
waitAndReturnElementListIfDisplayed(MENTIONPERSONNAME);
MobileElement mentionPersonNameSelect0 = mentionPersonNamesList.get(index0);
tapOnElement(mentionPersonNameSelect0);
sendKeyToElement(SENDMESSAGEFIELD, "#");
MobileElement mentionPersonNameSelect1 = mentionPersonNamesList.get(index1);
tapOnElement(mentionPersonNameSelect1);
tapOnElement(SENDMESSAGEBUTTON);
return this;
}
So the problem with the above code is the 2nd sendKeytoElement overwrites (i.e. clears) the previously filled text in the send message text field.
So what it does is the following:
types # -> selects user1 -> clears #user1 ->types # again -> selects #user2.
But what I want is to do the following:
types # -> selects user1 -> give a space ->types # again -> selects #user2
(i.e. DO NOT CLEAR #user1 FROM THE SEND MESSAGE TEXT FIELD)
Any help will be highly appreciated!

why the browser can not read the ID in Java with BDD eclipse however in other website is reading it with the same code

I have java code for one of my automation website and I copy that code to other PC to run other Website however the first step is running ok which is open the URL but the rest can not be run , unable to locate the element id , here is the code in Java:
#When("^I enter (.*) in the (.*)$")
public void i_enter_in_the_Username_txt_box(String emailaddress,String boxname) throws Throwable {
WebDriverWait wait = new WebDriverWait(driver,LOAD);
By id = By.id(boxname);
wait.until(ExpectedConditions.elementToBeClickable(id));
driver.findElement(By.id(boxname)).sendKeys(emailaddress);
}
BDD for this code is :
Feature: XYZ
Scenario: Add valid username and password then click on login button
Given User at login page // this one is working fine
When I enter Admin in the username // this one is not working , error saying that the Id for the username field can not find
And I enter Admin in the password
Then Home page will display
Would you please let me now if you know where is the issue ?
I tried the following but still i got the error :
Add Thread.sleep(1000); to wait but still i got the error
I added "" for the Admin and the Id for the field in the BDD file but still it did not work
I am using cucumber , Firefox , selenium , Eclipse , BDD, Maven and Java
Thanks a lot
Try change this driver.findElement(By.id(boxname)).sendKeys(emailaddress); to driver.findElement(id).sendKeys(emailaddress);
I found couple of issue. I am not clear what error you are getting that's reason I am trying to debug multiple way.
Feature: XYZ
Scenario: Add valid username and password then click on login button
Given User at login page // this one is working fine
When I enter Admin in the username // this one is not working , error saying that the Id for the username field can not find
And I enter Admin in the password
Then Home page will display
This is not correct. Given statement will work because you are not passing any value as parameters from statement to step definition.
It is failing here When I enter Admin in the username
because you didn't add parameter well.
I think you can do this way
When I enter "stack#stack.com" in the "username"
Currently It will take two user parameters as per your step definition requirement.
I am expecting "username" is your box name.
other reason could :
WebDriverWait wait = new WebDriverWait(driver,LOAD);
Are you sure you are getting driver and LOAD in step definition ?

Numeric value which is entered in a text filed with WebDriver using Java is deleted automatically

I am using Webdriver with java. I want to input some numeric value in a text field by using Java. I am using the following code:
driver.findElement(By.id("igtxtctl00_MasterPlaceHolder_WTxtZip")).sendKeys("25025");
But after entering the value completely, the entered value is erased automatically.
After the value is stored, I have to click on tab (using script), so that the City field is populated automatically after running the back end process.
I have achieved the same thing using Selenium RC by using below code:
selenium.typeKeys("igtxtctl00_MasterPlaceHolder_WTxtZip", "25025");
Thread.sleep(x);
selenium.keyPress("igtxtctl00_MasterPlaceHolder_WTxtZip", "9");
try some thing like this
driver.findElement(By.id("igtxtctl00_MasterPlaceHolder_WTxtZip")).sendKeys("25025");
after typing zip code just try to click some where else in the page
driver.findElement(By.id("igtxtctl00_MasterPlaceHolder_WTxtZip")).sendKeys(Keys.TAB);
OR
Try with Actions class.
new Actions(driver).sendKeys(driver.findElement(By.id("igtxtctl00_MasterPlaceHolder_WTxtZip")), "").perform();
From inspecting your code, I see that there is client-side logic based on that zip element. There is a post-back to the server to determine the city. The trigger to the post-back is the tab key.
This means that if you want to effectively test your page, you must send the tab key.

java.nio.file.Files.setOwner() not permitted

I am writing an attendance program in Java for use in class; my goal is to be able to have the students download the class file to their accounts (we'll be working on the same network) and then run them with their usernames and a password that will change daily. My program works fine, but I've run into a bug that I can't seem to overcome.
When it's run for the first time in any given day, attend.java will create a new text file in a specified directory on my account and then append the username of whoever ran the program to the file (currently it just appends the first argument of the program to the file, but I hope to modify it so it is more accurate and no one can give a false username), thereby compiling a list of students.
For example, running:
>> java attend desadams cheesecake
will take the given password "cheesecake" and if it matches the day's password either create a new textfile called "(today's date).txt" and write the username "desadams" to it (if it is being run for the first time that day) or else simply append the username "desadams" to the preexisting "(today's date).txt" file.
I wrote my own createFile() method that utilizes the java.nio.file package to create a file with custom permissions:
public static void createFile(File file) throws IOException{
Path newFile = file.toPath();
//create new file and set permissions
/* For the purposes of this program, the new file (the attendance list) must readable and writable to everbody
* in order for this program to work properly when run from their user, because this program both reads and modif\
ies.
*/
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrw-rw-");
Files.createFile(newFile);
Files.setPosixFilePermissions(newFile, perms);
//set owner
UserPrincipal owner = newFile.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("desadams");
Files.setOwner(newFile, owner);
//set group
GroupPrincipal group = newFile.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByGroupName("studen\
ts");
Files.getFileAttributeView(newFile, PosixFileAttributeView.class).setGroup(group);
}
I could not create the file and set the permissions at the same time, because the umask of the account would get in the way, so I did it in two steps and it works fine.
Now we get to my problem: I can compile and run this program just fine from my own account, but in anticipation of running it from other accounts, I changed the setOwner() method to use an arbitrary username instead of my own to better simulate the conditions of running the program from a different account. And it failed. Attempting to set the owner to anyone other than the person running the program results in an "operation not permitted" error.
I already knew that the UNIX command chown would not work because none of the network accounts have access to the root account, but I didn't expect it to prevent Java from doing the same thing, though I suppose it does make sense.
Is there anyway at all to get around this? The simplest way, of course, would be to run the program myself prior to anyone else each day, so that the attendance file is created in my own username and then I wouldn't have to worry about making sure to set the owner to my username within the program itself; it will always find the attendance file and thus not have to create it with the proper file attributes. However, though that may be exactly what I do, I would like to know if anyone has a solution that suggests how to set the owner within the program itself.
Thanks, and sorry this was a long post.
If the users are sudoers on your system, you could have them do sudo java attend desadams cheesecake. However, I expect they are not sudoers, and therefore your solution where you create the file first is probably the best one.

to read email password from text file and sigin using selenium rc and java

I have a email.txt file where I am storing all email address and password written in java and by running test through selenium rc. My question is that selenium .clicks sigIn then it should go to the text file email.txt and use one of the email address and password to signIn and second time when I run the selenium tests then it should not pick up the first time the email and password it has used for signing in.
As per my understanding about your question.You want to use all those email id and password one by one.
So for that scenario you better go with data driven testing.If you are using Junit use Parameterized runner to do the same.
If you are working with TestNG there is annotation known as #Dataprovider for data driven testing.
In this case your test case will be called n number of times for n different values of your test data(EmailID and password here in ur case) and eveery time new data will be used in that test.

Categories

Resources