Reputation: 13
I am new to Selenium Webdriver and trying to learn this API.I want to know how to effectively use multiple assertions in one test. I tried to use it directly but it is increasing my code length and also its very difficult to debug. Any suggestions how to get it done?
package com.eureqa.scripts;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Phase1 {
public static WebDriver driver;
public static WebDriver driver1;
public void navigation1(WebDriver driver1)
{
boolean result=verifyElementPresent(driver1);
if(result)
{
System.out.println("Element found");
}
else
{
System.out.println("Element not found");
}
driver1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
boolean result1=assertElementPresent(driver1);
if(result1)
{
System.out.println("Element asserted");
}
else
{
System.out.println("Element not asserted");
driver1.quit();
}
driver1.findElement(By.linkText("Reports")).click();
}
public boolean verifyElementPresent(WebDriver driver1)
{
try{
driver1.findElement(By.id("commonheader:headerForm:projectlist"));
return true;
}catch(Exception ex)
{
return false;
}
}
public boolean assertElementPresent(WebDriver driver1)
{
try{
driver1.findElement(By.linkText("Reports"));
return true;
}catch(Exception ex)
{
return false;
}
}
public static void main(String arr[]) throws InterruptedException
{
WebDriver driver1=LoginObject.driver();
System.out.println("Object Received");
LoginEureqa m=new LoginEureqa();
m.login(driver1);
Phase1 p1=new Phase1();
p1.navigation1(driver1);
System.out.println();
System.out.println("Phase1 executed successf`enter code here`ully");
}
}
Upvotes: 1
Views: 3032
Reputation: 11
Sometimes you may need multiple assertion for an add script.
Like,
Is this case you can handle both message like these following way:
String alert_Text = driver.switchTo().alert().getText();
String successMessage = "item successfully added.";
String duplicateMessage = "Duplicate item not allowed";
assertTrue(alert_Text.equals(successMessage) || alert_Text.equals(duplicateMessage), "Success or Duplicate both message will be count as successful message");
Upvotes: 1
Reputation: 7991
I've been combining my selenium tests with jUnit, so I can just use jUnit's built in assertions. It makes the code somewhat easier to use. The other method that you'll want to look at it Selenium's wait method.
As you're thinking about the tests, you should also consider whether or not you really need for the test to continue to run after an error condition is found. In your example, does it make sense for the test to try to click the "Reports" link, if both verifyElementPresent and assertElementPresent have failed? If you don't need to continue to run the test after a failure, your code will be simpler.
Looking at your code, I would probably rewrite navigation 1 something like:
package com.eureqa.scripts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Phase1 {
public static WebDriver driver;
public static WebDriver driver1;
public void navigation1(WebDriver driver1)
{
WebDriverWait wait = new WebDriverWait(driver, 10);
assertTrue("commonheader:headerForm:projectlist not found",
driver1.findElements(By.id("commonheader:headerForm:projectlist")).size() == 1);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Reports"))).click();
}
public static void main(String arr[]) throws InterruptedException
{
WebDriver driver1=LoginObject.driver();
System.out.println("Object Received");
LoginEureqa m=new LoginEureqa();
m.login(driver1);
Phase1 p1=new Phase1();
p1.navigation1(driver1);
System.out.println();
System.out.println("Phase1 executed successf`enter code here`ully");
}
}
Now, this doesn't exactly reproduce what you had in your original post, because it will fail at each step. If you wanted the first assertion to fail, but continue, you could surround the assertTrue with a try/catch block, and then display a separate message.
The code in navigation1 is pretty simple. It first tries to find an element with ID of "commonheader:headerForm:projectlist" If it doesn't find that element, it will throw a test error with the message of "commonheader:headerForm:projectlist not found".
The second step is to wait for a link to be clickable with the text "Reports" If that link doesn't appear within 10 seconds, an error will be thrown.
Upvotes: 1