Selenium WebDriver - Catch failure and continue with next step - java

I am using Selenium WebDriver with multiple test steps which are divided as the following:
//Example Test Step 1
driver.get("https://url.de");
driver.findElement(By.id("login")).clear();
driver.findElement(By.id("login")).sendKeys("username");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.cssSelector("#login-wrapper > input.button")).click();
driver.findElement(By.linkText("Your account")).click();
driver.findElement(By.cssSelector("button.link")).click();
driver.findElement(By.id("items_1")).click();
driver.findElement(By.id("items_2")).click();
driver.findElement(By.xpath("(//input[#id='items_1'])[1]")).click();
driver.findElement(By.xpath("(//input[#value='delete it'])[1]")).click();
driver.findElement(By.cssSelector("input.button.auth")).click();
System.out.println("[x] Test step has been performed sucessfully: ABO KUNDE");
If one test step fails, I want to catch this exception and force Selenium to continue with the next step. How can I handle this and do I have to define this handling for every single step?
So for example, the test step
driver.findElement(By.id("password")).sendKeys("password");
fails. Can I force Selenium to continue with "Example Test step 2"? We are using the Selenium WebDriver with BrowserStack.

Related

How can we get the results of assertion in Slim/Fitnesse

We have a fitnesse / slim table where in the fourth instruction is my assertion for the test case.is there a way we can get the result of this particular method as Pass or Fail.
For example
!|Fixture|
|method1|method2|method3|**method4**|
|res |res2 |res3 |**TCresult**|
For example here i want to know if method4 passed or failed.We have a different reporting system which cannot be done away with and every test case result needs to be validated.Also one test script have more that one test case in it.So we want the pass or fail of those methods which validate test cases.
script
given test page Testpage
and Test System setup is !define TEST_SYSTEM {slim}
!path lib/*.jar
and Setup content is
!|import|
|fitnesse.slim.test|
!|Scenario|Stop Test|
|start|ConstructorThrows|stop test|
!|Scenario|Not Executed|MESSAGE |
|check|echo|#MESSAGE|cannot fail #MESSAGE|
and Test content is
!|Script|
|Stop Test|
|Not Executed and should be ignored|
!|Script|
|Not Executed and should be ignored as well|
when page Testpage is tested and HTML is extracted
then \d assertions pass, 0 fail, 0 are ignored 1 exceptions thrown
and cell Stop Test has result error
and cell Not Executed and should be ignored has result ignore
and cell Not Executed and should be ignored as well has result ignore
and cell Test not run has result ignore
show Symbol $HTML_Input
show Symbol $HTML_Result
get collapsed executon log for page Testpage

Defined Step in Webdriver generates an Undefined step?

Considering the following Gherkin Scenario:
Scenario: This is a scenario 001
Given The user opens "website"
Then waits a few seconds
The first step goes OK: webdriver opens a browser and navigates to the start URL. Then, this happens:
Undefined step: Then waits a few seconds
Out of the blue, the step definition creates an extra space for no apparent reason... The java code looks like this:
#Then("^waits a few seconds$")
public void WaitsAFewSeconds() throws Throwable {
Thread.sleep(4000);
}
Who can tell me where this extra space is coming from??
Additional info:
- Normally, I write the Gherkin Code first, and press ALT+ENTER in IntelliJ to create the step definition. The steps for both gherkin lines are both present in the step definition file.

org.openqa.selenium.NoSuchElementException even when the element is present

I've 4 test cases defined using TestNG within my Appium project and all 4 test cases performs almost same code execution besides the difference that all 4 test cases run on different parts of the app I'm automating. The issue I'm facing is within my test case I need to find an element and get its value which works fine in Test Case 1 but fails for Test Case 2, 3 and 4 respectively. However if I comment out Test Case 1, then the code works fine for Test Case 2 but fails for Test Case 3 and 4. If I run code for Test Case 1, 2, 3 or 4 individually, all of them work. I'm not able to understand why the code works only for the first test case in execution but fails to find the element in the next test case.
Note that the only option for me to find element is using absolute xpath value since the app I'm trying to automate does not have any other attribute defined.
Environment
java client 4.1.2
Appium server version: v1.7.2
Selenium: v3.4.0
Desktop OS/version used to run Appium if necessary: Windows 10 x64
Mobile platform/version under test: Fire TV Stick running on Android 5.1.1
Real device or emulator/simulator: Real device
Details
I've provided the code for Test1 below. As mentioned earlier, Test2, Test3 and Test4 are same as Test1. The code below is the one which fails from second Test onwards even though the code works fine when tested individually and the element is definitely present on the screen when the code is executed. However the script returns no element found.
Failing code
strTime1 = driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.FrameLayout[3]/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TextView").getText();
Code To Reproduce Issue
#test //Test Case for Home tab
public void Test1_Home() throws InterruptedException, ParseException{
Thread.sleep(delay3); //Wait for 10 seconds to load the content
driver.findElementByXPath("//android.widget.FrameLayout[#content-desc="Home, Button"]").click(); //Code to click Home button
Thread.sleep(delay2); //Wait for 10 seconds to load the content
((AndroidDriver) driver).pressKeyCode(23); //Code to click first video thumbnail button
Thread.sleep(delay2); //Wait for 10 seconds to load the content
//Video pause verification code starts
((AndroidDriver) driver).pressKeyCode(23); //Pause the video
Thread.sleep(1000); //Wait for 1 seconds
strTime1 = driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.FrameLayout[3]/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TextView").getText();
System.out.println(strTime1);
Thread.sleep(delay1); //Wait for 5 seconds
strTime2 = driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.FrameLayout[3]/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TextView").getText();
System.out.println(strTime2);
time1 = time.parse(strTime1);
time2 = time.parse(strTime2);
System.out.println(time1);
System.out.println(time2);
if(String.valueOf(strTime1).equals(String.valueOf(strTime2)))
{
boolean result = true; //Set result to true
System.out.println("Home: Video playback pause successful");
Assert.assertTrue(result); //Passes the test case
}
else
{
boolean result = false; //Set result to false
System.out.println("Home: Video playback pause failed");
Assert.assertTrue(result); //Fails the test case
}
//Video pause verification code ends
Thread.sleep(delay2); //Allow the video to play for 10 seconds including buffering time
}
Exception stack traces
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.13 seconds
Let me know if any further information is required. Any kind of help would be appreciated
could you please try to locate the element with relative xpath or accesability id?
Also it is always best practice to use latest versions of appium server and java client as they are continuously making changes to the appium.
below is the link for latest java client if you are using maven : https://mvnrepository.com/artifact/io.appium/java-client

Selenium WebDriver tests sometimes doesn't find elements

I am pretty new to all the Automated Testing thing and recently I have been having trouble with selenium because sometimes fails finding certain input fields / buttons.
I added explicit waits for every element in the page, so the test is supposed to wait for that element to appear on screen, but sometimes randomly fails to do so. I will attach the latest error thrown by selenium in a simple test.
This is one of the tests that I am trying to run and randomly fails:
#Test
public void createTaskTest(){
generateTestEnvironmentSalesForceHomePage();
this.home.clickTasksTab();
generateTestEnvironmentTaskPage();
this.taskspage.clickNewTask();
generateTestEnvironmentNewTask();
this.newtask.setDateField("12/30/2016");
this.newtask.setCostField("1500");
this.newtask.setType("Personal Appointment");
this.newtask.setPaymentType("End of the day");
this.newtask.setDoctor("House");
this.newtask.submitTask();
Assert.assertTrue(this.newtask.checkDetailsPage());
}
Error thrown by selenium:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: span.dateOnlyInput>input (tried for 130 second(s) with 500 MILLISECONDS interval)
Here is the method to find the 'missing' date input:
public void setDateField(String date){
WebDriverWait wait = new WebDriverWait(this.driver,130);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.dateOnlyInput>input")));
this.driver.findElement(By.cssSelector("span.dateOnlyInput>input")).sendKeys(date);
}

run a test script 100 times in selenium webdriver

Currently I am using Selenium Webdriver, and for annotation I am using testNG, In my code I have 100 test scripts with #test annotation ..I just want to run only one of my test case 100 times ..how I can do it any suggestion with proper example is much appreciated eg
run only script number 5 hundred times
In the #Test annotation - you can add a invocationCount attribute with the number of times you want to run it.
#Test(invocationCount = 100)
public void testMethod() {
}

Categories