Selenium JUnit并行测试

Selenium JUnit并行测试,selenium,junit,Selenium,Junit,我试图运行一段我在网上找到的代码来并行运行JUnit测试,但我唯一修改的是访问本地主机的URL。问题是我经常会遇到以下错误,即使我已经设置了selenium grid很多次,并且知道如何设置路径等等。我不知道如何修复这个错误 org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.ie.driver system property; for mor

我试图运行一段我在网上找到的代码来并行运行JUnit测试,但我唯一修改的是访问本地主机的URL。问题是我经常会遇到以下错误,即使我已经设置了selenium grid很多次,并且知道如何设置路径等等。我不知道如何修复这个错误

org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver.
这是密码

package AutomationFrameWork;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.runners.Parameterized;
import org.junit.runners.model.RunnerScheduler;

public class Parallelized extends Parameterized {

  private static class ThreadPoolScheduler implements RunnerScheduler {
    private ExecutorService executor; 

    public ThreadPoolScheduler() {
      String threads = System.getProperty("junit.parallel.threads", "16");
      int numThreads = Integer.parseInt(threads);
      executor = Executors.newFixedThreadPool(numThreads);
    }

    @Override
    public void finished() {
      executor.shutdown();
      try {
        executor.awaitTermination(10, TimeUnit.MINUTES);
      } catch (InterruptedException exc) {
        throw new RuntimeException(exc);
      }
    }

    @Override
    public void schedule(Runnable childStatement) {
      executor.submit(childStatement);
    }
  }

  public Parallelized(Class<?> klass) throws Throwable {
    super(klass);
    setScheduler(new ThreadPoolScheduler());
  }
}

package AutomationFrameWork;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

@RunWith(Parallelized.class)
public class JUnitParallel {
  private String platform;
  private String browserName;
  private String browserVersion;
  private WebDriver driver;

  @Parameterized.Parameters
  public static LinkedList<String[]> getEnvironments() throws Exception {
    LinkedList<String[]> env = new LinkedList<String[]>();
    env.add(new String[]{Platform.WINDOWS.toString(), "chrome", "27"});
    env.add(new String[]{Platform.WINDOWS.toString(),"firefox","20"});
    env.add(new String[]{Platform.WINDOWS.toString(),"ie","11"});
    env.add(new String[]{Platform.WINDOWS.toString(),"safari","5.1.7"});

    //add more browsers here

    return env;
  }


  public JUnitParallel(String platform, String browserName, String browserVersion) {
    this.platform = platform;
    this.browserName = browserName;
    this.browserVersion = browserVersion;
  }



  @Before
  public void setUp() throws Exception {  
    DesiredCapabilities capability = new DesiredCapabilities();
    capability.setCapability("platform", platform);
    capability.setCapability("browser", browserName);
    capability.setCapability("browserVersion", browserVersion);
    capability.setCapability("build", "JUnit-Parallel");
    capability.setCapability("InternetExplorerDriverServer.IE_ENSURE_CLEAN_SESSION",true);
    System.setProperty("webdriver.ie.driver",
            "C:\\Users\\path to driver\\IEDriverServer.exe");
    driver = new RemoteWebDriver(
      new URL("http://localhost:7777".concat("/wd/hub")),
      capability
    );  
  }  

  @Test  
  public void testSimple() throws Exception {  
    driver.get("http://www.google.com");
    String title = driver.getTitle();
    System.out.println("Page title is: " + title);
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("BrowserStack");
    element.submit();
    driver = new Augmenter().augment(driver);
    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    try {
      FileUtils.copyFile(srcFile, new File("Screenshot.png"));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @After
  public void tearDown() throws Exception {  
    driver.quit();  
  }
}
包自动化框架;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.TimeUnit;
导入org.junit.runners.Parameterized;
导入org.junit.runners.model.RunnerScheduler;
公共类并行化扩展参数化{
私有静态类ThreadPoolScheduler实现RunnerScheduler{
私人遗嘱执行人;
公共线程池调度程序(){
String threads=System.getProperty(“junit.parallel.threads”,“16”);
int numThreads=Integer.parseInt(线程);
executor=Executors.newFixedThreadPool(numThreads);
}
@凌驾
公开作废已完成(){
executor.shutdown();
试一试{
执行人。等待终止(10,时间单位。分钟);
}捕获(中断异常exc){
抛出新的运行时异常(exc);
}
}
@凌驾
公共作废明细表(可运行的儿童声明){
执行人。提交(儿童声明);
}
}
公共并行化(类klass)抛出可丢弃{
超级(klass);;
setScheduler(新的ThreadPoolScheduler());
}
}
包装自动化框架;
导入java.io.File;
导入java.io.IOException;
导入java.net.URL;
导入java.util.LinkedList;
导入org.apache.commons.io.FileUtils;
导入org.junit.After;
导入org.junit.Before;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.junit.runners.Parameterized;
导入org.openqa.selenium.By;
导入org.openqa.selenium.OutputType;
导入org.openqa.selenium.Platform;
导入org.openqa.selenium.TakesScreenshot;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.remote.Augmenter;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入org.openqa.selenium.remote.RemoteWebDriver;
@RunWith(并行化的.class)
公共类JUnitParallel{
私有字符串平台;
私有字符串浏览器名;
私有字符串浏览;
私有网络驱动程序;
@参数化。参数化
公共静态LinkedList getEnvironments()引发异常{
LinkedList env=新建LinkedList();
添加(新字符串[]{Platform.WINDOWS.toString(),“chrome”,“27”});
添加(新字符串[]{Platform.WINDOWS.toString(),“firefox”,“20”});
add(新字符串[]{Platform.WINDOWS.toString(),“ie”,“11”});
添加(新字符串[]{Platform.WINDOWS.toString(),“safari”,“5.1.7”});
//在此处添加更多浏览器
返回环境;
}
公共JUnitParallel(字符串平台、字符串browserName、字符串browserVersion){
这个平台=平台;
this.browserName=browserName;
this.browserVersion=browserVersion;
}
@以前
public void setUp()引发异常{
DesiredCapabilities=新的DesiredCapabilities();
能力。设置能力(“平台”,平台);
setCapability(“浏览器”,浏览器名称);
setCapability(“browserVersion”,browserVersion);
setCapability(“构建”、“JUnit并行”);
setCapability(“InternetExplorerDriverServer.IE_sure_CLEAN_SESSION”,true);
System.setProperty(“webdriver.ie.driver”,

“C:\\Users\\driver\\IEDriverServer.exe的路径”); 驱动程序=新的RemoteWebDriver( 新网址(“http://localhost:7777“.concat(“/wd/hub”), 能力 ); } @试验 public void testSimple()引发异常{ 驱动程序。获取(“http://www.google.com"); 字符串title=driver.getTitle(); System.out.println(“页面标题为:“+标题”); WebElement=driver.findElement(By.name(“q”)); 元素sendKeys(“BrowserStack”); 元素。提交(); 驱动程序=新的增强器()。增强器(驱动程序); 文件srcFile=((TakesScreenshot)driver.getScreenshotAs(OutputType.File); 试一试{ copyFile(srcFile,新文件(“Screenshot.png”); }捕获(IOE异常){ e、 printStackTrace(); } } @之后 public void tearDown()引发异常{ driver.quit(); } }
“C:\\Users\\driver\\IEDriverServer.exe的路径”);“这是指向您的驱动程序的正确路径吗?我已将正确路径替换为‘指向驱动程序的路径’,但在我的机器上,它是正确的。请改为尝试使用1/斜杠。”