Maven测试并行配置仅打开一个浏览器

时间:2015-08-19 09:07:36

标签: maven selenium testng

嗨我试图在testng中并行执行测试它工作正常但是当我将它与maven集成时它只在一个浏览器中打开而且不会在我错误的地方请帮助,

java文件

public static WebDriver driver;
String baseURL = "http://www.google.com/";
long st;
long et;
long gdt;
@Parameters({ "browser" })
@BeforeTest
public void openBrowser(String browser) {
    try {
        if (browser.equalsIgnoreCase("Firefox")) {
             st=System.currentTimeMillis();
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
        } else if (browser.equalsIgnoreCase("chrome")) {

            System.setProperty("webdriver.chrome.driver",
                    "C:\\Temp\\imp\\chromedriver.exe");
             st=System.currentTimeMillis();
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }else if(browser.equalsIgnoreCase("phantom")){
            Capabilities caps = new DesiredCapabilities();

             ((DesiredCapabilities) caps).setJavascriptEnabled(true);                
           ((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
           ((DesiredCapabilities) caps).setCapability(
                   PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                   "C:\\Temp\\imp\\phantomjs.exe"); 
           st=System.currentTimeMillis();
           driver = new PhantomJSDriver(caps); 
           driver.manage().window().maximize();
        }       
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}
@Parameters({ "browser" })
@Test
public void login_TestCase_fireFox(String browser) throws IOException {
    if (browser.equalsIgnoreCase("Firefox")) {
    driver.get(baseURL);
    File scrFile1 = ((TakesScreenshot) driver)
            .getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile1,
            new File("c:\\screenshots\\fG.png"));

    }
}
@Parameters({ "browser" })
@Test
public void login_TestCase_chrome(String browser) throws IOException {
    if (browser.equalsIgnoreCase("chrome")) {
        driver.get(baseURL);
        File scrFile1 = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile1,
                new File("c:\\screenshots\\CG.png"));

    }
}

@Parameters({ "browser" })
@Test
public void login_TestCase_phantom(String browser) throws IOException {
if (browser.equalsIgnoreCase("phantom")) {
    driver.get(baseURL);
    File scrFile1 = ((TakesScreenshot) driver)
            .getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile1,
            new File("c:\\screenshots\\PG.png"));

}

TestNg.xml文件

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="4">
  <test name="Firefox Test">
  <parameter name="browser" value="Firefox"/>
    <classes>
      <class name="DemoParallel.Test.AppTest"/>
    </classes>
  </test>
  <test name="Chrome Test">
  <parameter name="browser" value="chrome"/>
    <classes>
      <class name="DemoParallel.Test.AppTest"/>
    </classes>
  </test>
    <test name="phantom Test">
  <parameter name="browser" value="phantom"/>
    <classes>
      <class name="DemoParallel.Test.AppTest"/>
    </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>DemoParallel</groupId>
  <artifactId>Test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Test</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>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <parallel>classes</parallel>
                    <threadCount>4</threadCount>
                    <systemPropertyVariables>
            <propertyName>firefox</propertyName>
          </systemPropertyVariables>
                </configuration>
            </plugin>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
        </plugins>
    </build>
  <dependencies>
    <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>
        <dependency>
    <groupId>com.github.detro.ghostdriver</groupId>
    <artifactId>phantomjsdriver</artifactId>
    <version>1.0.4</version>
</dependency>

  </dependencies>
</project>

MVN版本:

C:&gt; mvn -version

Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-1
3+05:30)
Maven home: C:\apache-maven-3.2.5
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_79\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

启动Firefox驱动程序时出错

java.lang.NoSuchMethodError: org.openqa.selenium.Proxy.extractFrom(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/Proxy;
  at org.openqa.selenium.firefox.FirefoxDriver.dropCapabilities(FirefoxDriver.java:313)
  at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)
  at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
  at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
  at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:95)

请帮忙,

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个我认为firefox版本和selenium版本不兼容试试这个

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
         <parallel>tests</parallel>
          <threadCount>10</threadCount>
           <systemPropertyVariables>
            <propertyName>Firefox</propertyName>
          </systemPropertyVariables>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>

        </configuration>
      </plugin>
        </plugins>
    </build>
  <dependencies>
    <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-firefox-driver</artifactId>
    <version>2.47.1</version>
</dependency>   
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>2.47.1</version>
</dependency>
        <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.47.1</version>
    </dependency>  
<dependency>
    <groupId>com.github.detro.ghostdriver</groupId>
    <artifactId>phantomjsdriver</artifactId>
    <version>1.0.4</version>
</dependency>

  </dependencies>