selenium unable to set cookie問題
在webDriver中,在一個webDriver登錄後,相應的cookies是通過登錄操作被保存到一個webDriver中,運行其他webDriver時,又需要做登錄操作,如果實現只登陸一次,然後共享cookies豈不是很好。
當嘗試添加cookies時拋出selenium unable to set cookie錯誤,大多情況是在一個webDriver空域(沒有訪問任意一個頁面)中添加cookies,我們要做的就是在賦值cookies之前保證webDriver不是一個空域,比如,讓它訪問一下需要共享頁面的主頁。
package cn.baopz.utils;
import cn.baopz.core.Authentication;
import cn.baopz.core.AuthenticationFactory;
import cn.baopz.core.LoginException;
import cn.baopz.core.WebDriverFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
public class ShareCookiesTest {
private Logger logger = LoggerFactory.getLogger(ShareCookiesTest.class);
private WebDriver mainDriver;
/**
* 在執行Test方法之前,登錄
*/
@Before
public void login() {
mainDriver = WebDriverFactory.getChromeWebDriver();
Authentication authentication = AuthenticationFactory.getInstance(mainDriver);
try {
Assert.assertTrue(authentication.check());
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (LoginException e) {
e.printStackTrace();
mainDriver.close();
mainDriver.quit();
}
}
/**
* 共享cookies
*/
@Test
public void shareCookies() {
WebDriver webDriver = WebDriverFactory.getChromeWebDriver();
//訪問同一個域中的地址
webDriver.get("https://xxx.xxx.xxx");
//cookie賦值
for (Cookie cookie : mainDriver.manage().getCookies()) {
logger.debug("{}:{}", cookie.getName(), cookie.getValue());
try {
webDriver.manage().addCookie(cookie);
} catch (Exception e) {
//ignore
}
}
//獲取資源詳情
webDriver.get("https://xxx.xxx.xxx/column/article/1234");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
webDriver.close();
webDriver.quit();
mainDriver.close();
mainDriver.quit();
}
}
selenium 截圖
Selenium截圖,當selenium運行時報錯,或異常時,需要記錄當前頁面情況,selenium提供截圖
package cn.baopz.utils;
import cn.baopz.core.WebDriverFactory;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class WebDriverTest {
private Logger logger = LoggerFactory.getLogger(WebDriverTest.class);
private WebDriver webDriver;
@Before
public void before() {
webDriver = WebDriverFactory.getChromeWebDriver();
}
@Test
public void takesScreenshotTest() {
webDriver.get("https://blog.csdn.net/bpz31456/article/details/81875735");
File source = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
try {
Files.copy(source.toPath(), new FileOutputStream(new File("C:\\Users\\baopz\\Desktop\\test\\image\\xx\\1.png")));
} catch (IOException e) {
e.printStackTrace();
}
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@After
public void after() {
webDriver.close();
webDriver.quit();
}
}