gpt4 book ai didi

java - Selenium 和 JUnit 在多个驱动程序中并行执行测试方法

转载 作者:行者123 更新时间:2023-12-01 10:58:24 24 4
gpt4 key购买 nike

现在:我有测试项目。使用 maven-failsave-plugin 运行 jetty,然后在那里部署 war 并执行测试脚本。TODO:我需要同时以一种方法在 5 个网络驱动程序中并行运行。或者同时在5个驱动程序中执行相同的测试类。这就像使用 Selenium 进行性能测试(不好的选择,但这是要求)。

最佳答案

这就是我的 ParallelParametrized 类的样子。

public class ParallelParametrized extends Parameterized {
private static class ThreadPoolScheduler implements RunnerScheduler {

private ExecutorService executor;

public ThreadPoolScheduler() {
String threads = System.getProperty("web.driver.amount");
int numThreads = Integer.parseInt(threads);
executor = Executors.newFixedThreadPool(numThreads);
}

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

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

public ParallelParametrized(Class klass) throws Throwable {
super(klass);
setScheduler(new ThreadPoolScheduler());
}

public void filter(Filter filter) throws NoTestsRemainException {
super.filter(new FilterDecorator(filter));
}

private static class FilterDecorator extends Filter {
private final Filter delegate;

private FilterDecorator(Filter delegate) {
this.delegate = delegate;
}

@Override
public boolean shouldRun(Description description) {
return delegate.shouldRun(wrap(description));
}

@Override
public String describe() {
return delegate.describe();
}
}

private static Description wrap(Description description) {
String name = description.getDisplayName();
String fixedName = deparametrizedName(name);
Description clonedDescription =
Description.createSuiteDescription(fixedName,description.getAnnotations().toArray(new Annotation[0]));
for(Description child : description.getChildren()){
clonedDescription.addChild(wrap(child));
}
return clonedDescription;
}

private static String deparametrizedName(String name) {
if(name.startsWith("[")){
return name;
}
int indexOfOpenBracket = name.indexOf('[');
int indexOfCloseBracket = name.indexOf(']')+1;
return name.substring(0,indexOfOpenBracket).concat(name.substring(indexOfCloseBracket));
}
}

那是我的抽象测试课

public abstract class AbstractParallelBaseTest {

public static ThreadLocal<WebDriver> CURRENT_DRIVER = new ThreadLocal<WebDriver>();

public static List<WebDriver> DRIVERS_TO_CLEANUP = Collections.synchronizedList(new ArrayList<WebDriver>());
public static final long TIMEOUT = 100;


protected static final Log LOGGER = LogFactory.getLog(AbstractParallelBaseTest.class);

static class WebDriverFactory {
static WebDriver create() {
WebDriver driver = null;
try {
LoggingPreferences logs = new LoggingPreferences();
logs.enable(LogType.BROWSER, Level.OFF);
logs.enable(LogType.CLIENT, Level.OFF);
logs.enable(LogType.DRIVER, Level.OFF);
logs.enable(LogType.PERFORMANCE, Level.OFF);
logs.enable(LogType.PROFILER, Level.OFF);
logs.enable(LogType.SERVER, Level.OFF);

DesiredCapabilities desiredCapabilities;
String webDriver = System.getProperty("web.driver");

if ("chrome".equals(webDriver)) {
desiredCapabilities = DesiredCapabilities.chrome();
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
desiredCapabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxy));
driver = new ChromeDriver(desiredCapabilities);
} else if ("ie".equals(webDriver)) {
desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
desiredCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
desiredCapabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxy));
driver = new InternetExplorerDriver(desiredCapabilities);
} else if ("firefox".equals(webDriver)) {
desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
driver = new FirefoxDriver(desiredCapabilities);
} else {
desiredCapabilities = DesiredCapabilities.htmlUnitWithJs();
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
driver = new HtmlUnitDriver(desiredCapabilities);
}

driver.manage().window().setPosition(new Point(0, 0));
driver.manage().window().setSize(new Dimension(1650, 1080));
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}

return driver;
}
}

@Parameterized.Parameter
public int currentDriverIndex;

@Parameterized.Parameters(name = "{0}")
public static ArrayList<Object> driverTypes() {
return Lists.newArrayList(getParams());
}
}

在每个测试的实现上,我粘贴@RunWith(ParallelParametrized.class)并且它有效!

关于java - Selenium 和 JUnit 在多个驱动程序中并行执行测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33480102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com