如何在TestNG中实施有条件的测试运行?

时间:2017-11-02 06:31:00

标签: selenium-webdriver automation testng

我陷入了一种情况,即允许用户每天只使用相同的用户凭据对电影评分一次。 如果用户尝试为同一部电影或合同评分,则会看到错误pop_up。

我希望以某种方式实施,即如果任何电影/合约被评级。应跳过评级功能,并应处理错误弹出。

我正在使用Selenium eclipse 2017,Chrome浏览器61.0和Test-Ng

请帮助。

感谢。

public class Ratings {
String driverPath = "F:/ChromeDriver/chromedriver.exe";
public WebDriver driver;
public Alert alert;

@BeforeTest
public void LaunchBrowser () throws InterruptedException {
System.out.println("WebBrowser open");
System.setProperty("webdriver.chrome.driver","F:/ChromeDriver/chromedriver.e
xe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority = 1, alwaysRun = true)
public void HomePageUSA() throws InterruptedException {
    driver.navigate().to("Https://us.justdial.com");
    String expectedTitle = "Justdial US";
    String actualTitle = driver.getTitle();
    try
    {
        AssertJUnit.assertEquals(expectedTitle, actualTitle);
    System.out.println("Test Passed");
    }
    catch (Throwable e)
    {
    System.out.println("Test Failed");
    }
    Thread.sleep(3000);
}
@Test (priority = 2, dependsOnMethods = {"HomePageUSA"})
public void Login() throws Exception{
    Thread.sleep(3000);
driver.findElement(By.xpath("/html/body/div/div[1]/div[1]/div[1]/div/div/div
/div[4]/aside/div/span/a[1]")).click();
driver.findElement(By.id("inputPassword3")).clear();
driver.findElement(By.id("inputPassword3")).sendKeys("testing.testjd@gmail.c
om");
driver.findElement(By.id("exampleInputPassword1")).clear();
driver.findElement(By.id("exampleInputPassword1")).sendKeys("justdial");
driver.findElement(By.xpath("/html/body/div[4]/div[2]/div[1]/section/div/div
[1]/div/form/div[3]/div/button")).click();
Thread.sleep(1000);
String expectedTitle = "Justdial US";
String actualTitle = driver.getTitle();
try
{
    Assert.assertEquals(expectedTitle, actualTitle);
System.out.println("Login Successful");
}
catch (Throwable e)
{
System.out.println("Login Failed");
}
Thread.sleep(1000);
driver.findElement(By.xpath(".//*[@id='us-jdnew-
wrapper']/div[1]/div/header/div/div[1]/a[2]")).click();
Thread.sleep(2000);
}
  @Test (priority = 3)
  public void Movies_Rating_page() throws Exception {
  driver.findElement(By.xpath(".//*[@id='hotkeylnk106']/div[2]")).click();
  Thread.sleep(2000);
  driver.findElement(By.xpath(".//*[@id='main-

wrapper']/div/div/div[3]/div[2]/div/div[1]/div[1]/div/a/span/img")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='main-
wrapper']/div/div/div[2]/div[1]/ul/li[2]/span/a[2]/span[1]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath(".//*
[@id='AlreadyRated']/div/div/div/section/div/a")).click();
System.out.println("Rating Page Redirection Successful");
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='us-jdnew-
wrapper']/div/form/div/div/div/div[2]/span[2]/span[10]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath(".//*[@id='us-jdnew-
wrapper']/div/form/div/div/div/div[3]/div[3]/textarea")).sendKeys("Very nice 
movie, Must watch.");   
Thread.sleep(1000); 
driver.findElement(By.xpath(".//*[@id='us-jdnew-
wrapper']/div/form/div/div/div/div[3]/div[4]/button[2]")).click();
Thread.sleep(3000);
System.out.println("Rating Successfully Submitted");

1 个答案:

答案 0 :(得分:1)

您可以创建方法并将测试方法中的方法标记为dependsOnMethods。你可以像下面那样实现它(我试图根据提供的信息回答最好的)

这里的想法是,当满足您的评分条件时,isMovieRated应抛出异常,以便testNG跳过Movies_Rating_page(),否则isMovieRated只会返回true而不会应该跳过。

       @Test
       public static boolean isMovieRated(String locator) {

  //check in "if" below that element has already clicked or is equal to something. I used 'AlreadyClicked' just to 
     give an idea as I dont have your application information.

    if (driver.findElement(By.xpath(locator).getText()=="AlreadyClicked"){
     throw new RuntimeException();
    }
    else {

    return true;

    }
}

现在您的Movies_Rating_page()将如下所示

@Test (priority = 3,dependsOnMethods = { "isMovieRated" })
  public void Movies_Rating_page() throws Exception {

   public static String YourLocator = "/html/body/...."
   Ratings.isMovieRated(YourLocator);
   ..
   }

here是有关testNG dependsOnMethods

的更多信息的链接

注意:

  1. 上述代码未经过测试。
  2. 如果您正在检查Movies_Rating_page()中的评分以外的事情,那么您应该将这些内容分开,因为抛出异常时会跳过所有内容。

    希望这会有所帮助。