有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

selenium中的JavaTestNG建议使用哪些注释

我在Eclipse中创建了如下项目(页面对象模型)

项目名称 包1 src 箱子 方案2 src 垃圾箱

包1中包含元素说明和方法 在包2中包含:

基本脚本。爪哇--------- 预断

webdriver driver=new FirefoxDriver();

        driver.get("url");
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        driver.manage().window().maximize();
        LoginPage l=new LoginPage(driver);
        l.setusername("");
        l.setpassword("");
        l.LoginBut();

后置条件

司机。退出()

我有T1。爪哇,T2。java并转换为。xml(testng.xml)并使用testng文件(testng.xml)运行

我想用一个浏览器一次执行所有的测试用例,但当我执行测试用例时,它会调用BaseScript。爪哇


共 (1) 个答案

  1. # 1 楼答案

    Selenium是一种像用户一样控制浏览器/网站的工具。它模拟用户点击页面。了解web应用程序的功能后,可以设置测试。现在一起运行一组测试用例,即测试套件TestNG提供了管理测试执行的功能

    我建议您阅读这个简单的tutorial来设置TestNG测试套件

    I want to execute all testcases at a time

    Selenium Grid是Selenium套件的一部分,用于并行运行测试。在基类中设置驱动程序

    public class TestBase {
    
    protected ThreadLocal<RemoteWebDriver> threadDriver = null;
    
    @BeforeMethod
    public void setUp() throws MalformedURLException {
    
        threadDriver = new ThreadLocal<RemoteWebDriver>();
        DesiredCapabilities dc = new DesiredCapabilities();
        FirefoxProfile fp = new FirefoxProfile();
        dc.setCapability(FirefoxDriver.PROFILE, fp);
        dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
        threadDriver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc));
    }
    
    public WebDriver getDriver() {
        return threadDriver.get();
    }
    
    @AfterMethod
    public void closeBrowser() {
        getDriver().quit();
    
    }
    }
    

    样本测试的一个例子是:

    public class Test01 extends TestBase {
    
    @Test
    public void testLink()throws Exception {
        getDriver().get("http://facebook.com");
        WebElement textBox = getDriver().findElement(By.xpath("//input[@value='Name']"));
        // test goes here
    }
    }
    

    您可以以与上面类似的方式添加更多测试

    public class Test02 extends TestBase {
    
     @Test
     public void testLink()throws Exception {
        // test goes here
     }
    }
    

    TestNG配置:

    测试。xml

    <suite name="My Test Suite">
     <suite-files>
        <suite-file path="./testFiles.xml" />
     </suite-files>
    

    测试文件。xml

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Parallel test runs" parallel="tests" thread-count="2">
    
    <test name="T_01">
      <classes>
         <class name="com.package.name.Test01" ></class>
      </classes>
    </test>
    
    <test name="T_02">
      <classes>
         <class name="com.package.name.Test02" ></class>
      </classes>
    </test>
    
    <!  more tests  >
    
    </suite>