简体   繁体   English

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

[英]How to run tests cases in one test

im looking for solution to run test where are included test cases from several test cases.我正在寻找运行测试的解决方案,其中包含来自多个测试用例的测试用例。 I need to make test like this: 1.open homepage 2.click on login link 3. type login and pass + submit我需要像这样进行测试:1.打开主页 2.点击登录链接 3.输入登录并通过+提交

I have 2 classes (pages) HomePage and LoginPage, I want to make GotoLoginPageTest and LoginTest.我有 2 个类(页面)HomePage 和 LoginPage,我想制作 GotoLoginPageTest 和 LoginTest。 I dont know how to make flow to run GotoLoginPageTest and in the same browser LoginTest (2 tests cases in one test).我不知道如何让流程运行 GotoLoginPageTest 并在同一浏览器 LoginTest 中运行(一个测试中有 2 个测试用例)。 I dont know how to addict this two tests cases.我不知道如何上瘾这两个测试用例。 Can You tell me how do do this, or some example using pagefactory, page object?你能告诉我怎么做吗,或者一些使用 pagefactory、page 对象的例子? Im using maven, testng, java.我使用 maven、testng、java。 My CODE:我的代码:

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

}

my testng.xml我的 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 pass but LoginTest fail, I have error java.lang.NullPointerException. 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)

I know that in LoginPage is no Instanse of driver, but I do not want to create new driver (browser) becouse I want make this tests in one, the same browser.我知道 LoginPage 中没有驱动程序实例,但我不想创建新驱动程序(浏览器),因为我想在同一个浏览器中进行此测试。

I was trying use super(driver) but then even tests from GotoLoginPage was fails...我正在尝试使用 super(driver) 但即使来自 GotoLoginPage 的测试也失败了......

I just want to know what to do to make possible run few testscases in one test using project structure like I use now (pages classes + test classes)我只想知道如何使用像我现在使用的项目结构(页面类 + 测试类)在一个测试中运行几个测试用例

Base Class:基类:

       public class BaseClass {
    public static WebDriver driver;

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

}

HomePage:主页:

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

LoginPage:登录页面:

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

------======================UPLOADED 22 oct 2015================------- ------====================== 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);
}
}

HomePage主页

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

New ERROR!!!:新错误!!!:

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

As you are using TESTNG , it is very simple to run multiple methods/Class by one click.当您使用 TESTNG 时,一键运行多个方法/类非常简单。

You do not need different class for Home page , Login etc. You can do like this :您不需要为主页、登录等设置不同的类。您可以这样做:

1 - Create common TestNG class 1 - 创建通用的 TestNG 类

2 - Add Function/Method for Home page 2 - 为主页添加功能/方法

3 - Add Function/Method for Login 3 - 添加登录功能/方法

METHOD 1 :方法一:

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

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

If you want to use your current classes then just convert your existing classes to TESTNG class and add one new TESTNG class to call method of those classes.如果您想使用当前的类,那么只需将现有类转换为 TESTNG 类并添加一个新的 TESTNG 类来调用这些类的方法。

METHOD 2 :方法二:

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

}

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

}

So this is how you can run multiple things in selenium using TESTNG by one click and one class only.因此,这就是您可以使用 TESTNG 通过一键单击和一个类在 selenium 中运行多个事物的方法。

METHOD 3 :方法三:

In Addition you can run test via XML also , That steps are given below :此外,您还可以通过 XML 运行测试,步骤如下:

1 - Create new xml file by right click on your project. 1 - 通过右键单击您的项目创建新的 xml 文件。

2 - Paste below code in 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>

Above will allow you to run all tests by just one xml.以上将允许您仅通过一个 xml 运行所有测试。

As per your updated question , Your driver getting null as declaration and calling classes are different.根据您更新的问题,您的驱动程序获取 null 作为声明和调用类是不同的。

So you should declare driver : public static WebDriver driver;所以你应该声明 driver : public static WebDriver driver;

Then when you are calling to other class then it will be like : GotoLoginPageTest.driver.getTitle();然后,当您调用其他类时,它将类似于: GotoLoginPageTest.driver.getTitle(); Also you should remove all driver declaration from all other classes once you apply above.此外,一旦您在上面应用,您应该从所有其他类中删除所有驱动程序声明。

One more thing that for to declare driver globally , you can create base class and add method in a way like :要全局声明驱动程序的另一件事,您可以通过以下方式创建基类和添加方法:

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

To call it in other class , it will be Base.getdriver().yourproperties要在其他类中调用它,它将是Base.getdriver().yourproperties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM