How to priorities Testcases in Selenium Webdriver using TestNG
To priorities testcase we need to set priority to every testcase. In below example we have prioritized Login Testcase first and Location Testcase second. We have used "priority" keyword in Test annotation. Test execution starts with 0 priority.
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class LoginLinked {
FirefoxDriver driver = new FirefoxDriver();
@BeforeClass
public void BrowserIntiate()
{
driver.get("https://www.linkedin.com");
driver.manage().window().maximize();
}
@Test(priority=0) // It will execute first as its priority is zero
public void Login()
{
WebElement txtUserName = driver.findElementById("login-email");
txtUserName.sendKeys("Username");
WebElement txtPassword = driver.findElementById("login-password");
txtPassword.sendKeys("Password");
WebElement btnLogIn = driver.findElementByName("submit");
btnLogIn.click();
}
@Test(priority=1)
public void LocationTest()
{
WebElement lnkAdvance = driver.findElementByLinkText("Advanced");
lnkAdvance.click();
WebElement lnkClose = driver.findElementByXPath("//button[@class='text-button close-advs']");
lnkClose.click();
WebElement chkCountryIN = driver.findElementById("in:0-G-ffs");
chkCountryIN.click();
Assert.assertEquals("INDI", chkCountryIN.getText());
}
@AfterClass
public void BrowserQuit()
{
driver.quit();
}
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class LoginLinked {
FirefoxDriver driver = new FirefoxDriver();
@BeforeClass
public void BrowserIntiate()
{
driver.get("https://www.linkedin.com");
driver.manage().window().maximize();
}
@Test(priority=0) // It will execute first as its priority is zero
public void Login()
{
WebElement txtUserName = driver.findElementById("login-email");
txtUserName.sendKeys("Username");
WebElement txtPassword = driver.findElementById("login-password");
txtPassword.sendKeys("Password");
WebElement btnLogIn = driver.findElementByName("submit");
btnLogIn.click();
}
@Test(priority=1)
public void LocationTest()
{
WebElement lnkAdvance = driver.findElementByLinkText("Advanced");
lnkAdvance.click();
WebElement lnkClose = driver.findElementByXPath("//button[@class='text-button close-advs']");
lnkClose.click();
WebElement chkCountryIN = driver.findElementById("in:0-G-ffs");
chkCountryIN.click();
Assert.assertEquals("INDI", chkCountryIN.getText());
}
@AfterClass
public void BrowserQuit()
{
driver.quit();
}
}