[Selenium+Java] TestNG: Execute multiple Test Suites

來源:互聯網
上載者:User

標籤:started   snap   void   action   out   test   分享   gmail   increase   

Original URL: https://www.guru99.com/testng-execute-multiple-test-suites.html

TestNG: Execute multiple Test Suites


TestNG enables you to run test methods, test classes and test cases in parallel inside your project. By performing parallel execution, we can reduce the ‘execution time‘ as tests are started and executed simultaneously in different threads.

Here we will see how to run multiple classes (aka different suites) using TestNG.

Creating a TestNG.xml file for executing test

In order to do that follow the below steps.

  1. Create a new project in eclipse
  2. Create two packages in the projects (name them as com.suite1 and com.suite2)
  3. Create a class in each package (name them as Flipkart.java and Snapdeal.java) and copy the below code in respective classes
  4. Create a new file in your project and name it as testing.xml (Make sure you‘ve installed testing plugin for eclipse, instructions available here). Testng.xml contains all configuration (classnames, testnames and suitnames.

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 passwrodString password = "";// This method is to navigate flipkart URL@BeforeClasspublic void init() {driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);driver.navigate().to("https://www.flipkart.com");}// To log in flipkart@Testpublic 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@Testpublic 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 resultsString 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();}@Testpublic 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();}@Testpublic 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();}@AfterClasspublic 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 passwrodString password = "";String pinCode = "";// This method is to navigate flipkart URL@BeforeClasspublic void init() {driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);driver.navigate().to("https://www.snapdeal.com");}// To log in flipkart@Testpublic 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@Testpublic 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 resultsString css = ".product_grid_row:nth-of-type(1)>div:nth-child(1)";driver.findElement(By.cssSelector(css)).click();}@Testpublic 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();}@Testpublic 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();}@AfterClasspublic 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>

Final project structure looks like below,

Parallel execution in TestNG

After creating xml file as shown above, in next step, we will execute the parallel test. Below is the code.

1) thread-count: This is used for parallel execution, based on the number script. It will execute in parallel or sequential order.

2) verbose: It is used to log the execution details in the console. The value should be 1-10. The log details in the console window will get more detailed and clearer as you increase the value of the verbose attribute in the testng.xml configuration file.

3) name: Name of the suite. Here it is "Gmail Suite"

4) Parallel: To run scripts parallel, value can be tests/classes/methods/suites. Default value is none

Right click on the testing.xml and select run as testing, once successful you‘ll see all the results

When you execute the above code, you will get the following output.

Output:

1 name of the suite given in testng.xml

2 name of the test given in testng.xml

3 name of the class given in testng.xml

4 method names annotated with @Test in .java file

Likewise, it will execute test suite for snap deal as well.

Conclusion:

Here we have seen how to use Testng to execute parallel test. TestNG gives an option to execute multiple test in parallel in a single configuration file (XML).

[Selenium+Java] TestNG: Execute multiple Test Suites

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.