一尘不染

使用Java和TestNG在WebDriver上同时在不同的操作系统和浏览器上执行测试

selenium

我已经在系统中配置了网格并编写了测试脚本。我可以在任何指定的操作系统和任何浏览器上运行测试,但一次只能在一个操作系统和一个浏览器上运行测试,而不是同时运行所有操作系统和所有浏览器。这是我所做的。请告诉我如何配置它,以便它可以一次在已配置的操作系统中运行。

我的使用Java的脚本如下:

import java.net.MalformedURLException;
import java.net.URL;
import org.junit.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;

public class GridWithWebdriver {

    public WebDriver driver;

    @Parameters({"browser"})
    @BeforeClass
    public void setup(String browser) throws MalformedURLException {
        DesiredCapabilities capability=null;

        if(browser.equalsIgnoreCase("firefox")){
            System.out.println("firefox");
            capability= DesiredCapabilities.firefox();
            capability.setBrowserName("firefox"); 
            capability.setPlatform(org.openqa.selenium.Platform.ANY);
            //capability.setVersion("");
        }

        if(browser.equalsIgnoreCase("iexplore")){
            System.out.println("iexplore");
            capability= DesiredCapabilities.internetExplorer();
            capability.setBrowserName("iexplore"); 
            capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
            //capability.setVersion("");
        }

        driver = new RemoteWebDriver(<span class="IL_AD" id="IL_AD11">new URL</span>("http://localhost:4444/wd/hub"), capability);
        driver.navigate().to("http://google.com");

    }

    @Test
    public void test_first() throws InterruptedException{
        Thread.sleep(3000);
        WebElement search_editbox   =   driver.findElement(By.name("q"));
        WebElement search_button    =   driver.findElement(By.name("btnG"));
        search_editbox.clear();
        search_editbox.sendKeys("first");
        search_button.click();
    }

    @Test
    public void test_second(){
        WebElement search_editbox   =   driver.findElement(By.name("q"));
        WebElement search_button    =   driver.findElement(By.name("btnG"));
        search_editbox.clear();
        search_editbox.sendKeys("second");
        search_button.click();
    }

    @AfterClass
    public void tearDown(){
        driver.quit();
    }
}

Testng.xml:

<suite name="Selenium <span class="IL_AD" id="IL_AD7">Grid with</span> webdriver" verbose="3"  parallel="classes" thread-count="2">   
  <test name="Selenium Grid Demo">
  <parameter name="browser" value="iexplore"/>
    <classes>
      <class name="tests.GridWithWebdriver"/>
      <class name="tests.GridWithWebdriver1"/>
    </classes>
 </test>
 </suite>

阅读 273

收藏
2020-06-26

共1个答案

一尘不染

阿迪亚

您要在不同或相同系统上运行并行测试的代码(testng.xml)应该如下所示:

Testng.xml

 <!DOCTYPE suite SYSTEM "Http://testng.org/testng-1.0.dtd">
    <suite name="My Sample Suite" verbose="3"  parallel="tests" thread-count="2">


      <test name="Run on Firefox">
        <parameter name="mybrowser"  value="firefox"/>
        <parameter name="myip"  value="http://172.16.10.119:5566/wd/hub"/>
        <classes>
          <class name="testcases.Login"/>
        </classes>
     </test>  
    <test name="Run on Chrome">
        <parameter name="mybrowser"  value="chrome"/>
        <parameter name="myip"  value="http://172.16.10.106:5567/wd/hub"/>
        <classes>
          <class name="testcases.Login"/>
        </classes>
     </test> 
     </suite>

here i am trying to acess one linux(ttp://172.16.10.119:5566) and one mac(http://172.16.10.106:5567) and sending its ip and browser as a parameter. To run it parallel i have mentioned in my <testsuite> tag as parallel="tests" thread-count="2"

我希望你现在很清楚。

2020-06-26