需要帮助在 junit 运行中使用 cucumber、selenium、maven、TestNG 运行跨浏览器(多浏览器)测试

Posted

技术标签:

【中文标题】需要帮助在 junit 运行中使用 cucumber、selenium、maven、TestNG 运行跨浏览器(多浏览器)测试【英文标题】:Need help to run Cross-browser(Multi Browser) Testing using cucumber,selenium, maven , TestNG in junit run 【发布时间】:2017-12-14 05:20:51 【问题描述】:

请参阅下面的代码并帮助打破我的 Parentdriver 类以供多个驱动程序使用。我现在累了。这段代码在 chrome 中以 junit 和 maven 正确运行

Parendriver.java

package com.maxreqman.framework;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public abstract class ParentDriver 

    public static WebDriver driver;


    public  WebDriver getDriver() 

        if (driver == null) 
            System.setProperty("webdriver.chrome.driver",
           "ChromeDriver/chromedriver.exe");            
            ChromeOptions cOptions = new ChromeOptions();
            cOptions.addArguments("--start-maximized");
            cOptions.addArguments("--disable-web-security");
            cOptions.addArguments("--disable-notifications");
            cOptions.addArguments("--no-proxy-server");
            Map<String, Object> prefs = new HashMap<String, Object>();
            prefs.put("credentials_enable_service", false);
            prefs.put("profile.password_manager_enabled", false);
            cOptions.setExperimentalOption("prefs", prefs);
            cOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
            driver = new ChromeDriver(cOptions);
            driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);


        
        return driver;
    


ParenScenario.java

package com.maxreqman.framework;

import org.openqa.selenium.WebDriver;

import com.maxreqman.pageObject.LogIn;

public class ParentScenario extends ParentDriver 

    protected WebDriver driver = getDriver();

    public LogIn login;

    public void startBrowser() 
        login = new LogIn(driver);
    

    public void navigateTo() 
          driver.navigate().to("http://www.myurl.com");

    


LogIn_StepDefinition.java

package com.maxreqman.glue;

import com.maxinvman.framework.ParentScenario;

import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class LogIn_StepDefinitions extends ParentScenario 

    @Before
    public void beforeScenario() throws Throwable 
        startBrowser();
    

    @Given("^User is on LogIn page$")
    public void user_is_on_LogIn_page() throws Throwable 
        navigateTo();
    

    @When("^User enters \"([^\"]*)\" into Username field$")
    public void user_enters_into_Username_field(String UName) throws Throwable 
        Thread.sleep(2000);
        login.enterUsername(UName);
    

    @And("^User enters \"([^\"]*)\" into Password field$")
    public void user_enters_into_Password_field(String Pass) throws Throwable 
        Thread.sleep(2000);
        login.enterPassword(Pass);
    

    @And("^User Click LogIn button$")
    public void user_Click_LogIn_button() throws Throwable 
        Thread.sleep(2000);
        login.clickButton();
    


LogIn.java(PageObject)

package com.maxreqman.pageObject;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.maxinvman.framework.ParentDriver;

public class LogIn extends ParentDriver 

    WebDriver driver = getDriver();

    public LogIn(WebDriver driver) 
        this.driver = driver;
    

    public void enterUsername(String UName) 
        driver.findElement(By.xpath(".//*[@id='Username']")).sendKeys(UName);
    

    public void enterPassword(String Pass) 
        driver.findElement(By.xpath(".//*[@id='password']")).sendKeys(Pass);
    

    public void clickButton() 
        driver.findElement(By.xpath("//*[@id='login']/div[5]/button")).click();
        

跑步者(Junit)

package com.maxreqman.runner;

import java.io.File;

import org.junit.AfterClass;
import org.junit.runner.RunWith;
import com.cucumber.listener.Reporter;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/Feature_All/1A_LoginValidationCheck/",
        glue = "com.maxreqman.glue",
        plugin = "com.cucumber.listener.ExtentCucumberFormatter:output/report.html",
        format = "json:target/cucumber.json","html:target/A_Features"
        )
public class Runner extends AbstractTestNGCucumberTests 

    @AfterClass
    public static void setup() 
        Reporter.loadXMLConfig(new File("src/test/resources/extentreports/extent-config.xml"));
        Reporter.setSystemInfo("user", System.getProperty("user.name"));
        Reporter.setSystemInfo("os", "Windows 10 Pro");
        Reporter.setTestRunnerOutput("Max Requisition Managment Testing");
       

TestNG XML

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Test Suite">

    <test name="Test Cases Suite">
        <classes>
            <class name="com.maxreqman.runner.Runner"></class>
        </classes>
    </test>

</suite>

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cucumber</groupId>
  <artifactId>MAXRequisitionManagement</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MaxReqManTesting</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>

    <plugins>        
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.6.1</version>
         <configuration>
           <source>1.8</source>
           <target>1.8</target>
         </configuration>
     </plugin>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
              <testFailureIgnore>true</testFailureIgnore>
              <suiteXmlFiles>
                <suiteXmlFile>src/test/resources/testng/testng.xml</suiteXmlFile>
              </suiteXmlFiles>
        </configuration>
     </plugin>
     <plugin>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.7</version>
    </plugin>
    <plugin>
        <groupId>net.masterthought</groupId>
        <artifactId>maven-cucumber-reporting</artifactId>
        <version>3.7.0</version>
        <executions>
            <execution>
                <id>execution</id>
                <phase>verify</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <projectName>MaxReqMan</projectName>
                    <outputDirectory>$project.build.directory/cucumber-report-html</outputDirectory>
                    <cucumberOutput>$project.build.directory/cucumber.json</cucumberOutput>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>

  </build>

  <dependencies>
    <dependency>
        <groupId>com.browserstack</groupId>
        <artifactId>browserstack-local-java</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>com.vimalselvam</groupId>
        <artifactId>cucumber-extentsreport</artifactId>
        <version>2.0.4</version>
    </dependency>

  </dependencies>

</project>

1A_LoginValidationCheck.feature

作者:Md.Borhan Uddin Sarker,

关键字摘要:申请测试框架

Feature: User Test LogIn

  Scenario: Unsuccessful Login with Invalid Username and Valid Password
    Given User is on LogIn page
    When User enters "mithukhan18@yahoo.com" into Username field
    And User enters "123" into Password field
    And User Click LogIn button
    And Login Denied With Alert Message "Invalid Email or Password"

  Scenario: Unsuccessful Login with Valid Username and Invalid Password
    Given User is on LogIn page
    When User enters "fahadbilla@yahoo.com" into Username field
    And User enters "123d34593234556" into Password field
    And User Click LogIn button
    And Login Denied With Alert Message "Invalid Email or Password"

  Scenario: Unsuccessful Login with Blank Username and Blank Password
    Given User is on LogIn page
    When User enters "" into Username field
    And User enters "" into Password field
    And User Click LogIn button
    And Login Denied With Alert Message "Please enter valid email address"

  Scenario: Unsuccessful Login with Blank Username and Valid Password
    Given User is on LogIn page
    When User enters "" into Username field
    And User enters "123" into Password field
    And User Click LogIn button
    And Login Denied With Alert Message "Please enter valid email address"

  Scenario: Unsuccessful Login with Valid Username and Blank Password
    Given User is on LogIn page
    When User enters "fahadbillah@yahoo.com" into Username field
    And User enters "" into Password field
    And User Click LogIn button
    And Login Denied With Alert Message "Please enter password"

  Scenario: Successful Login with Valid Username and Valid Password
    Given User is on LogIn page
    When User enters "fahadbillah@yahoo.com" into Username field
    And User enters "123" into Password field
    And User Click LogIn button
    And User Login Successfully

【问题讨论】:

你到底卡在哪里了?你看到什么错误?谢谢 这个项目已经完全启动并正在运行......但我一次只能使用一个驱动程序(chrome/firefox)。我想用多个驱动程序和并行运行它。请看 parentdriver.java 类....你能帮我把它转换成在多驱动程序中运行吗...谢谢 【参考方案1】:

您可以在 pom.xml 中添加 selenium grid 依赖项。依赖如下:

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium.grid/selenium-grid-hub -->
<dependency>
    <groupId>org.seleniumhq.selenium.grid</groupId>
    <artifactId>selenium-grid-hub</artifactId>
    <version>1.0.5</version>
</dependency>

在您的脚本中,您需要为 mozilla 设置 geckodriver,为 chrome 浏览器设置 `chromedriver'。只需定义驱动程序在您的机器中保存的路径。

【讨论】:

以上是关于需要帮助在 junit 运行中使用 cucumber、selenium、maven、TestNG 运行跨浏览器(多浏览器)测试的主要内容,如果未能解决你的问题,请参考以下文章

Junit +cucumber 运行报错 initiallizationError

JUnit 测试和集成测试 - Jenkins - Cucumber

Cucumber JUnit 测试不将功能粘合到步骤定义

将 JUnit Hooks 与 Cucumber CLI Runner 一起使用

并行执行没有发生在Cucumber JVM 4.0.0和Junit测试运行器上

springboot+mybatis+cucumber