如何 运行 在一次测试中测试用例

How to run tests cases in one test

我正在寻找 运行 测试的解决方案,其中包含来自多个测试用例的测试用例。 我需要像这样进行测试: 1.open 主页 2.click 登录时 link 3. 输入登录并通过 + 提交

我有2个类(页)HomePage和LoginPage,我想制作GotoLoginPageTest和LoginTest。我不知道如何在同一浏览器中进入 运行 GotoLoginPageTest 和 LoginTest(一次测试中有 2 个测试用例)。 我不知道如何让这两个测试用例上瘾。你能告诉我怎么做吗,或者一些使用 pagefactory,页面对象的例子? 我正在使用 maven、testng、java。 我的代码:

 public class HomePage {
 WebDriver driver;  

 public static final  String PAGE_TITLE = "page title";
 public static final  String PAGE_URL = "www.blbl.pl";

@FindBy(xpath = "//*[@id='global-nav']/div/div/div[3]/ul/li[1]/a")
WebElement LogInLink;

    public HomePage(WebDriver driver){
        this.driver = driver;
    }

    public void isHomePage(){
    String pageTitle = driver.getTitle();
    Assert.assertEquals(pageTitle, PAGE_TITLE);
}

public void goToLoginPage(){
    LogInLink.click();
}

}

登录页面

public class LoginPage {
WebDriver driver;

public static final  String PAGE_TITLE = "Login";

@FindBy(id="user_email")
WebElement inputUserEmail;

@FindBy(id="user_password")
WebElement inputUserPassword;


public LoginPage(WebDriver driver){
    this.driver = driver;
}

public void isLoginPage(){
    String pageTitle = driver.getTitle();
    Assert.assertEquals(pageTitle, PAGE_TITLE);
}

public void fillUserEmail(){
    inputUserEmail.sendKeys("asdfasd@gmail.com");
    Assert.assertEquals(inputUserEmail.getAttribute("value"), "asdfasd@gmail.com");
}

public void fillUserPassword(){
    inputUserPassword.sendKeys("123456");
    Assert.assertEquals(inputUserPassword.getAttribute("value"), "123456");
}

}

GotoLoginPageTest

import pages.HomePage;

public class GotoLoginPageTest {
    WebDriver driver;
    HomePage hp;


    @BeforeClass
    public void setup(){
        this.driver = new FirefoxDriver();
        hp = PageFactory.initElements(driver, HomePage.class);
        driver.get(HomePage.PAGE_URL);
    }

    @Test(priority = 1)
    public void isHomePage(){
        hp.isHomePage();
    }

    @Test(dependsOnMethods = "isHomePage")
    public void gotoLoginPage(){
        hp.goToLoginPage();
    }

}

登录测试

public class LoginTest {
    WebDriver driver;

    LoginPage lp = PageFactory.initElements(driver, LoginPage.class);

    @Test
    public void cheskIsLoginPage(){
        lp.isLoginPage();
    }

    @Test
    public void logInBase(){
        lp.fillUserEmail();
        lp.fillUserPassword();
    }

}

我的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="tests.GotoLoginPageTest"/>
      <class name="tests.LoginTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

GotoLoginPageTest 通过 但是 LoginTest 失败,我有错误 java.lang.NullPointerException。

java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy5.sendKeys(Unknown Source)
at pages.LoginPage.fillUserEmail(LoginPage.java:30)
at tests.LoginTest.logInBase(LoginTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1032)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

我知道 LoginPage 中没有驱动程序实例,但我不想创建新的驱动程序(浏览器),因为我想在同一个浏览器中进行此测试。

我尝试使用 super(driver) 但后来甚至来自 GotoLoginPage 的测试都失败了...

我只想知道如何使用像我现在使用的项目结构(页面 类 + 测试 类)

基础Class:

       public class BaseClass {
    public static WebDriver driver;

    public BaseClass(WebDriver driver) {
        BaseClass.driver = driver;
    }

}

首页:

public HomePage(WebDriver driver){
            super(driver);
        }

登录页面:

public HomePage(WebDriver driver){
            super(driver);
        }

------======================2015 年 10 月 22 日上传============= ===------


基地Class

public class BaseClass {
    public static WebDriver driver; --------------- CHANGED
public String PAGE_URL;
public String PAGE_TITLE;

public BaseClass(WebDriver driver) {
    BaseClass.driver = driver;---------------------- CHANGED
    PageFactory.initElements(driver, this);
}
}

首页

public class HomePage extends BaseClass {

    public HomePage(WebDriver driver) {
        super(driver);
        this.PAGE_TITLE = "title";
        this.PAGE_URL = "https://totest.com/";
    }

     @FindBy(xpath = "//*[@id='global-nav']/div/div/div[3]/ul/li[1]/a")
     WebElement LogInLink;

    public void isHomePage(){
        String pageTitle = driver.getTitle();
        Assert.assertEquals(pageTitle, PAGE_TITLE);
    }

    public void goToLoginPage(){
        LogInLink.click();
    }
}

登录页面

public class LoginPage extends BaseClass{

    @FindBy(id="user_email")
    WebElement inputUserEmail;

    @FindBy(id="user_password")
    WebElement inputUserPassword;

    public LoginPage(WebDriver driver){
        super(driver);
        this.PAGE_TITLE = "Login to Base";
    }

    public void isLoginPage(){
        String pageTitle = driver.getTitle();
        Assert.assertEquals(pageTitle, PAGE_TITLE);
    }

    public void fillUserEmail(){
        inputUserEmail.sendKeys("mail@gmail.com");
        Assert.assertEquals(inputUserEmail.getAttribute("value"), "mail@gmail.com");
    }

    public void fillUserPassword(){
        inputUserPassword.sendKeys("testbase");
        Assert.assertEquals(inputUserPassword.getAttribute("value"), "testbase");
    }

}

转到登录页面

public class GotoLoginPageTest {
------------------------------------------ CHANGED(removerd WebDriver driver)

    public HomePage hp;

    @BeforeTest
    public void setup(){
        driver = new FirefoxDriver();
        hp = PageFactory.initElements(driver, HomePage.class);
        driver.get(hp.PAGE_URL);
    }

    @Test(priority = 1)
    public void isHomePage(){
        hp.isHomePage();
    }

    @Test(dependsOnMethods = "isHomePage")
    public void gotoLoginPage(){
        hp.goToLoginPage();
    }

}

登录测试

public class LoginTest {
    ------------------------------------------ CHANGED(removerd WebDriver driver)
    public LoginPage lp;

    public void setup(WebDriver driver){
    lp = PageFactory.initElements(driver, LoginPage.class);
    }

    @Test
    public void cheskIsLoginPage(){
        lp.isLoginPage();   
    }

    @Test
    public void logInBase(){
        lp.fillUserEmail();
        lp.fillUserPassword();
    }

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="tests.GotoLoginPageTest"/>
      <class name="tests.LoginTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

新错误!!!:

TestNG] Running:
  /tmp/testng-eclipse-1551592337/testng-customsuite.xml

PASSED: isHomePage
PASSED: gotoLoginPage
FAILED: cheskIsLoginPage
java.lang.NullPointerException
    at tests.LoginTest.cheskIsLoginPage(LoginTest.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:771)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
    at org.testng.SuiteRunner.run(SuiteRunner.java:259)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1032)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

FAILED: logInBase
java.lang.NullPointerException
    at tests.LoginTest.logInBase(LoginTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:771)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
    at org.testng.SuiteRunner.run(SuiteRunner.java:259)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1032)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    Default test
    Tests run: 4, Failures: 2, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 4, Failures: 2, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6d86b085: 9 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 3 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@39a054a5: 6 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@6a38e57f: 4 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1f89ab83: 17 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@43556938: 4 ms

因为你用的是TESTNG,一键运行多个methods/Class非常简单

主页、登录等不需要不同的class。您可以这样做:

1 - 创建通用 TestNG class

2 - 为主页添加 Function/Method

3 - 为登录添加 Function/Method

方法一:

    @Test(priority = 0)
    public void Home() 
    {
      
       //LOGIC FOR HOME
    
    }

    @Test(priority = 1)
    public void Login() 
    {
      
       //LOGIC FOR LOGIN
    
    }

如果你想使用你当前的 classes 那么只需将你现有的 classes 转换为 TESTNG class 并添加一个新的 TESTNG class 来调用方法那些 classes.

方法二:

@Test(priority = 0)
public void Home() 
{
  
  //  Yourclassname.(dot)method name

}

@Test(priority = 1)
public void Login() 
{
  
   //  Yourclassname.(dot)method name

}

所以这就是您如何使用 TESTNG 运行 selenium 中的多个东西,只需单击一次,然后 class。

方法三:

此外,您还可以通过 XML 运行 进行测试,步骤如下:

1 - 通过右键单击您的项目创建新的 xml 文件。

2 - 在 xml

中粘贴以下代码
  <suite name="Your Test Suite Name">
    <test name="Your Test Name>
        <classes>
            <class name="Yourpackage.(dot)classname"/> //This is that common class which we have created above
        </classes>
    </test>
</suite>

以上将允许您 运行 所有测试只需一个 xml。

根据您更新的问题,您的驱动程序在声明中获取 null 和调用 classes 是不同的。

所以你应该声明驱动程序:public static WebDriver driver;

然后当你调用其他 class 时,它会像:GotoLoginPageTest.driver.getTitle(); 此外,一旦你在上面应用,你应该从所有其他 classes 中删除所有驱动程序声明。

还有一件事要全局声明驱动程序,您可以创建基础 class 并以如下方式添加方法:

public static WebDriver driver;
        
        
        //CHECK FOR DRIVER AND CALL IT
        public static WebDriver getdriver()
        {
            
            if(driver==null)
            {
                
                setup();
                
                
            }
            
            return driver;
            
        }

要在其他 class 中调用它,它将是 Base.getdriver().yourproperties