我一直致力于自动化测试,并尝试跳过登录页面,一次登录,将cookie保存在文本文件中,然后最终在新的浏览器实例上读取和添加文件。
当我一次只创建一个浏览器时,它工作正常,但如果我创建多个浏览器并行,则只有一个页面接收cookie(因此按预期跳过登录页面)。
这是代码:
我的设置()。这很完美。我只需登录,将cookie数据保存在文本文件中并关闭浏览器。
@BeforeClass
public void cookie_setup(){
//Firefox
System.setProperty("webdriver.gecko.driver", "C:\\SeleniumGecko/geckodriver.exe");
webdriver = new FirefoxDriver();
File file = new File("Cookies.data");
try
{
file.delete();
file.createNewFile();
FileWriter fileWrite = new FileWriter(file);
BufferedWriter Bwrite = new BufferedWriter(fileWrite);
for ( Cookie ck: webdriver.manage().getCookies())
{
Bwrite.write((ck.getName()+";"+ck.getValue()+";"+ck.getDomain()+";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure()));
Bwrite.newLine();
}
Bwrite.close();
fileWrite.close();
}catch (Exception ex)
{
ex.printStackTrace();
}
cookies = webdriver.manage().getCookies();
webdriver.quit();
}
@Test(threadPoolSize = 4, invocationCount = 4)
public void search() throws InterruptedException {
System.setProperty("webdriver.gecko.driver",
"C:\\SeleniumGecko/geckodriver.exe");
webdriver.navigate().to("http://pagethatrequirelogin/");
try {
File file = new File("Cookies.data");
FileReader fileReader = new FileReader(file);
BufferedReader Buffreader = new BufferedReader(fileReader);
String strline;
while ((strline = Buffreader.readLine()) != null) {
StringTokenizer token = new StringTokenizer(strline, ";");
while (token.hasMoreTokens()) {
String name = token.nextToken();
String value = token.nextToken();
String domain = token.nextToken();
String path = token.nextToken();
Date expiry = null;
String val;
if (!(val = token.nextToken()).equals("null")) {
expiry = new Date(val);
}
Boolean isSecure = Boolean.valueOf(token.nextToken());
Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);
System.out.println(ck);
webdriver.manage().addCookie(ck);
}
}
}
catch(Exception ex){
}
}我使用TestNG和参数:
@Test(threadPoolSize = 4, invocationCount = 4)
我同时创建4个firefox浏览器,但只有一个浏览器收到cookie并跳过登录页面。
如果我一次多次创建一个浏览器,它的工作完全正常。
问题是因为您可能正在使用@Test
方法覆盖webdriver实例。这是一个示例,展示了它的线程安全版本:
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
public class CookiesSample {
private static final ThreadLocal<WebDriver> drivers = new ThreadLocal<>();
private List<Cookie> cookies = new ArrayList<>();
@BeforeClass
public void beforeClass() throws IOException {
String text = "Cookies.data";
List<String> lines = Files.readAllLines(Paths.get(text));
lines.forEach(line -> {
StringTokenizer token = new StringTokenizer(line, ";");
while (token.hasMoreTokens()) {
String name = token.nextToken();
String value = token.nextToken();
String domain = token.nextToken();
String path = token.nextToken();
Date expiry = null;
String val = token.nextToken();
if (!val.equals("null")) {
expiry = asDate(val);
}
Boolean isSecure = Boolean.valueOf(token.nextToken());
Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);
cookies.add(ck);
}
});
}
@BeforeMethod
public void beforeMethod() {
WebDriver driver = new FirefoxDriver();
cookies.forEach(driver.manage()::addCookie);
drivers.set(driver);
}
@AfterMethod
public void afterMethod() {
drivers.get().quit();
drivers.remove();
}
@Test(threadPoolSize = 4, invocationCount = 4)
public void testMethod() {
drivers.get().get("http://pagethatrequirelogin/");
}
private static Date asDate(String text) {
try {
return DateFormat.getInstance().parse(text);
} catch (ParseException e) {
return new Date();
}
}
}