【问题标题】:Two tests one after another两个测试一个接一个
【发布时间】:2016-01-16 23:55:29
【问题描述】:

我在 WebDriver 中的测试有问题。

在第一个包中,我有 2 个类(页面)HomePage、LoginPage。

在第二个包中我有测试 - goToLiginPageTest 和 LoginTest。

在 goToLiginPage 我检查,我在主页上并转到登录页面。

在 LoginTest 中,我检查我是否在登录页面并登录。

但来自 goToLiginPageTest 的两个测试通过,但来自 LoginTest 的测试失败。

我不确定我的 textng.xlm 有什么问题,或者我哪里出错了。请帮帮我。

    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 -->

我有错误

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)

【问题讨论】:

    标签: java selenium selenium-webdriver testng


    【解决方案1】:

    您从未在 LoginTest 类中实例化驱动程序。在那里实例化驱动程序并将其传递到页面对象中。像这样的:

    public class LoginTest {
        WebDriver driver;
        driver = new FirefoxDriver();
    
        LoginPage lp = PageFactory.initElements(driver, LoginPage.class);
    
        @Test
        public void cheskIsLoginPage(){
            lp.isLoginPage();
        }
    
        @Test
        public void logInBase(){
            lp.fillUserEmail();
            lp.fillUserPassword();
        }
    }
    

    【讨论】:

    • 但我不想创建新的 FFdriver,我希望在进入登录页面后登录。在同一个测试中。我只是想一个接一个地运行 GotoLoginPageTest 和 LoginTest。我不确定,但我认为测试运行顺序不好,首先是 LoginTest,然后是 GotoLoginPageTest。我把来自 eclipse zapodaj.net/c776122be852e.png.html 的屏幕放在这里
    • 您必须这样做,否则您将向登录页面传递一个空驱动程序。另一方面,您应该在全局范围内创建驱动程序,而不是在每个测试对象中创建。参见示例here
    • 抱歉我的问题,但我不想在每个测试中创建新的 firefoxDriver 对象。我只想在一个测试中运行两个案例的地方进行测试。如果我创建新的ffdriver,那么将打开新的浏览器,我想在一个浏览器中做这两种情况,一个接一个
    • 要么将测试放在同一个类中,要么您需要在 2 个类之间共享驱动程序,否则我看不到第一个类的实例如何在没有任何附加代码的情况下注入第二个类...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多