如何在Selenium中运行多个TestSuite

TestNG使能够在项目中并行运行测试方法、测试类和测试用例。通过并行执行,减少执行时间。 在这里,将看到如何使用TestNG运行多个类(也就是不同的套件)。

步骤1) 创建用于执行测试的TestNG.xml文件

以下步骤:

  1. 在eclipse中创建新项目
  2. 在项目中创建两个包(com.suite1和com.suite2)
  3. 在每个包中创建一个类(Flipkart.java和SnapDeal.java),并将以下代码复制到各自的类中
  4. 在项目中创建一个新文件,并将其命名为testng.xm。xml包含所有配置(类名、测试名和套件名)。

Flipkart.java

 package com.suite1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Flipkart {
    WebDriver driver = new FirefoxDriver();
    String username = ""; // Change to your username and passwrod
    String password = "";
    // This method is to navigate flipkart URL
    @BeforeClass
    public void init() {
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.navigate().to("https://www.flipkart.com");
    }
    // To log in flipkart
    @Test
    public void login() {
        driver.findElement(By.partialLinkText("Login")).click();
        driver.findElement(
            By.cssSelector(".fk-input.login-form-input.user-email"))
        .sendKeys(username);
        driver.findElement(
            By.cssSelector(".fk-input.login-form-input.user-pwd"))
        .sendKeys(password);
        driver.findElement(By.cssSelector(".submit-btn.login-btn.btn")).click();
    }
    // Search For product
    @Test
    public void searchAndSelectProduct() {
        driver.findElement(By.id("fk-top-search-box")).sendKeys("moto g3");
        driver.findElement(
            By.cssSelector("search-bar-submit.fk-font-13.fk-font-bold"))
        .click();
        // select the first item in the search results
        String css = ".gd-row.browse-grid-row:nth-of-type(1) > div:nth-child(1) >div>div:nth-child(2) >div>a";
        driver.findElement(By.cssSelector(css)).click();
    }
    @Test
    public void buyAndRemoveFromCart() {
        driver.findElement(
            By.cssSelector(".btn-express-checkout.btn-big.current"))
        .click();
        driver.findElement(By.cssSelector(".remove.fk-inline-block")).click();
        Alert a = driver.switchTo().alert();
        a.accept();
    }
    @Test
    public void logout() {
        Actions s = new Actions(driver);
        WebElement user = driver.findElement(By.partialLinkText(username));
        s.moveToElement(user).build().perform();
        driver.findElement(By.linkText("Logout")).click();
    }
    @AfterClass
    public void quit() {
        driver.close();
    }
}
 

SnapDeal.java

 package com.suite2;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class SnapDeal {
    WebDriver driver = new FirefoxDriver();
    String username = ""; // Change to your username and passwrod
    String password = "";
    String pinCode = "";
    // This method is to navigate snapdeal URL
    @BeforeClass
    public void init() {
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.navigate().to("https://www.snapdeal.com");
    }
    // To log in flipkart
    @Test
    public void login() {
        driver.findElement(By.xpath("//button[text()='Login']")).click();
        driver.switchTo().frame("loginIframe");
        driver.findElement(By.cssSelector("div[onClick='getLoginForm()']"))
        .click();
        driver.findElement(By.id("j_username")).sendKeys(username);
        driver.findElement(By.id("j_password_login")).sendKeys(password);
        driver.findElement(By.id("signin_submit")).click();
        driver.switchTo().defaultContent();
    }
    // Search For product
    @Test
    public void searchAndSelectProduct() {
        driver.findElement(By.cssSelector(".col-xs-20.searchformInput.keyword"))
        .sendKeys("iphone 6s");
        driver.findElement(By.cssSelector(".sd-icon.sd-icon-search")).click();
        // select the first item in the search results
        String css = ".product_grid_row:nth-of-type(1) >div:nth-child(1) ";
        driver.findElement(By.cssSelector(css)).click();
    }
    @Test
    public void buyAndRemoveFromCart() {
        driver.findElement(By.xpath("//li[contains(text(),'Silver')]")).click();
        driver.findElement(By.id("pincode-check")).sendKeys(pinCode);
        driver.findElement(By.id("buy-button-id")).click();
 
        driver.findElement(By.cssSelector("i[title='Delete Item']")).click();
        Alert a = driver.switchTo().alert();
        a.accept();
    }
    @Test
    public void logout() {
 
        driver.findElement(By.linkText("START SHOPPING NOW")).click();
        Actions s = new Actions(driver);
        WebElement user = driver.findElement(By.cssSelector(".sd-icon.sd-icon-user"));
        s.moveToElement(user).build().perform();
        driver.findElement(By.linkText("Logout")).click();
    }
    @AfterClass
    public void quit() {
        driver.close();
    }
}
 

TestNg.xml

 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose="1" name="Gmail Suite" annotations="JDK" parallel="tests">
 <test name="flipkart">
  <classes>
   <class name="com.suite1.Flipkart"/>
  </classes>
 </test>
 <test name="Myntra">
   <classes>
    <class name="com.suite2.SnapDeal"/>
   </classes>
 </test>
</suite>
 

最终的项目结构如下所示,

TestNG: Execute multiple Test Suites
TestNG: Execute multiple Test Suites

步骤2) TestNG中的并行执行

如上所示创建XML文件后,在下一步,我们将执行并行测试。以下是代码。

TestNG: Execute multiple Test Suites
TestNG: Execute multiple Test Suites
  1. thread-count :这用于基于数字脚本的并行执行。它将并行或顺序执行。

  2. verbose :用于在控制台记录执行详情。随着testng.xml配置文件中Verbose属性值的增加,控制台窗口中的日志详细信息将变得更加详细和清晰。

  3. name :套件的名称

  4. Parallel :要并行运行脚本,值可以是tests/class/method/suites。默认值为 无

右键单击testng.xml并选择Run As Testing,一旦成功,将看到所有结果。

输出:

TestNG: Execute multiple Test Suites
TestNG: Execute multiple Test Suites
  1. testng.xml中给出的套件名称
TestNG: Execute multiple Test Suites
TestNG: Execute multiple Test Suites
  1. testng.xml中给出的测试名称
TestNG: Execute multiple Test Suites
TestNG: Execute multiple Test Suites
  1. testng.xml中给出的类名
TestNG: Execute multiple Test Suites
TestNG: Execute multiple Test Suites
  1. .java文件中带有@Test标注的方法名称
TestNG: Execute multiple Test Suites
TestNG: Execute multiple Test Suites

同样,它还将执行Snap Deal的测试套件。

IT赶路人

专注IT知识分享