javascript 我可以使用 selenium-webdriver 一次(同时)运行多个实例吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33741921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I run multiple instances at once(simultaneously) with selenium-webdriver?
提问by yaquawa
I'm trying to use Selenium to automate file uploading.
我正在尝试使用 Selenium 自动上传文件。
I'v already wrote a tiny program with the selenium-webdriverthat works.
我已经用selenium-webdriver编写了一个可以工作的小程序。
The problem is, there are thousands of files need to be uploaded, I'd like to run multiple browser instances simultaneously to speed up the automation. So I tried something like this
问题是,有数千个文件需要上传,我想同时运行多个浏览器实例以加快自动化速度。所以我尝试了这样的事情
var i = 0;
while (i < 10) {
i++;
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
// login and upload files......
}
I expected this would create 10 browser instances at once, and do the automation simultaneously.
我预计这会一次创建 10 个浏览器实例,并同时进行自动化。
But actually... the above code will creates browser instance 'one by one' which means, it won't create another instance until the previous one finishes.
但实际上......上面的代码将“一个一个”地创建浏览器实例,这意味着,在前一个完成之前它不会创建另一个实例。
I'v also tried execute the program in multiple shell instances, that will fire up multiple browser instances for me, but I just don't want to do this...
我还尝试在多个 shell 实例中执行该程序,这将为我启动多个浏览器实例,但我只是不想这样做......
采纳答案by Shamik
Well you need to create multiple threads instead of looping, then you can start each upload in parallel threads. You are on the right track. You dont need selenium grid to achieve this.
那么你需要创建多个线程而不是循环,然后你可以在并行线程中开始每个上传。你在正确的轨道上。你不需要硒网格来实现这一点。
lookup about multithreading. You can start with this answer
查找多线程。你可以从这个答案开始
It's not right you need grid for executing multiple browser sessions. You can invoke multiple browser sessions by just creating multiple driver objects, and managing them. Each session will be separate if you want them to be.
您需要网格来执行多个浏览器会话是不对的。您可以通过创建多个驱动程序对象并管理它们来调用多个浏览器会话。如果您愿意,每个会话都将是单独的。
Grid is for scaling as there is a limitation on the no of browser instances you can run keeping your machine performance intact and tests stable. Like more than 5 chrome instances in a single machine. If you want to do more than that then you have to use selenium Grid.
网格用于扩展,因为您可以运行的浏览器实例数量有限制,以保持机器性能不变并测试稳定。就像在一台机器中超过 5 个 chrome 实例。如果你想做更多,那么你必须使用 selenium Grid。
回答by user2272115
This is exactly the purpose of Selenium Grid.
这正是Selenium Grid的目的。
回答by moefinley
You should create a new instance of the WebDriver and it's capabilities for each new browser you want to open.
您应该为每个要打开的新浏览器创建 WebDriver 的新实例及其功能。
The following will open Google in five separate instances of Chrome.
以下将在五个单独的 Chrome 实例中打开 Google。
import * as webdriver from "selenium-webdriver";
import * as Chrome from 'selenium-webdriver/chrome';
function loadSelenium(){
let options = new Chrome.Options();
let capabilities = options.toCapabilities();
console.log('loading another');
return new webdriver.Builder()
.forBrowser('chrome')
.withCapabilities(capabilities)
.build();
}
for(let i = 0; i < 5; i++) {
let driver = loadSelenium();
driver.get('http://www.google.com');
}
回答by MAG DAG
parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
parallel="methods":TestNG 将在单独的线程中运行所有测试方法。相关方法也将在单独的线程中运行,但它们会遵守您指定的顺序。
parallel="tests": TestNG will run all the methods in the same tag in the same thread, but each tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
parallel="tests":TestNG 将在同一个线程中运行同一个标签中的所有方法,但每个标签将在一个单独的线程中。这允许您将所有不是线程安全的类组合在一起,并保证它们都将在同一线程中运行,同时利用 TestNG 使用尽可能多的线程来运行您的测试。
parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
parallel="classes":TestNG 将在同一个线程中运行同一个类中的所有方法,但每个类将在单独的线程中运行。
parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
parallel="instances":TestNG 会在同一个线程中运行同一个实例中的所有方法,但是两个不同实例上的两个方法将在不同的线程中运行。