需要帮助运行跨浏览器(多浏览器)在junit运行中使用黄瓜,硒,maven,TestNG进行测试

时间:2017-07-10 12:53:52

标签: testing selenium-webdriver junit cross-browser cucumber

请参阅下面的代码并帮助打破多个驱动程序的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

1 个答案:

答案 0 :(得分:0)

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

<!-- 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'。只需定义驱动程序在计算机中保存的路径即可。