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

时间:2015-10-20 06:04:33

标签: selenium testing selenium-webdriver pageobjects

我正在寻找运行测试的解决方案,其中包括来自多个测试用例的测试用例。 我需要像这样进行测试: 1.打开主页 2.点击登录链接 3.输入login并传递+ submit

我有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();
}

}

LoginPage

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();
    }

}

LoginTest

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中没有Instanse的驱动程序,但我不想创建新的驱动程序(浏览器)因为我想在同一个浏览器中进行此测试。

我正在尝试使用super(驱动程序)但是后来甚至GotoLoginPage的测试都失败了......

我只想知道如何使用像我现在使用的项目结构(页面类+测试类)在一个测试中运行少量测试用例

基类:

       public class BaseClass {
    public static WebDriver driver;

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

}

首页:

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

LoginPage:

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

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

BaseClass的

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();
    }
}

LoginPage

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");
    }

}

GotoLoginPage

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();
    }

}

LoginTest

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

1 个答案:

答案 0 :(得分:0)

当您使用TESTNG时,只需单击一下即可运行多个方法/ Class非常简单。

您不需要为主页,登录等设置不同的课程。您可以这样做:

1 - 创建常见的TestNG类

2 - 为主页添加功能/方法

3 - 添加登录功能/方法

方法1:

    @Test(priority = 0)
    public void Home() 
    {

       //LOGIC FOR HOME

    }

    @Test(priority = 1)
    public void Login() 
    {

       //LOGIC FOR LOGIN

    }

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

方法2:

@Test(priority = 0)
public void Home() 
{

  //  Yourclassname.(dot)method name

}

@Test(priority = 1)
public void Login() 
{

   //  Yourclassname.(dot)method name

}

这就是你可以通过一次点击和一个课程使用TESTNG在selenium中运行多个东西。

方法3:

另外,您也可以通过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。

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

然后,当你打电话给其他班级时,它会像:GotoLoginPageTest.driver.getTitle();你也应该在上面申请后从所有其他班级中删除所有司机声明。

为了全局声明驱动程序,还可以通过以下方式创建基类和添加方法:

public static WebDriver driver;


        //CHECK FOR DRIVER AND CALL IT
        public static WebDriver getdriver()
        {

            if(driver==null)
            {

                setup();


            }

            return driver;

        }

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