Selenium WebDriver 处理cookie
在使用webdriver测试中,很多地方都使用登陆,cookie能够实现不必再次输入用户名密码进行登陆。
首先了解一下Java Cookie类的一些方法。
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.Cookie; public class Cookies { public static void main(String[] args) { WebDriver driver=DriverFactory.create(); driver.get("http://selenium.jd-app.com/wp-login.php"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); WebElement user = driver.findElement(By.xpath("//*[@id=‘user_login‘]")); user.clear(); user.sendKeys("username"); WebElement password = driver.findElement(By .xpath("//*[@id=‘user_pass‘]")); password.clear(); password.sendKeys("password"); WebElement submit=driver.findElement(By.xpath("//*[@id=‘wp-submit‘]")); submit.submit(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } File file=new File("broswer.data"); try { //delete file if exists file.delete(); file.createNewFile(); FileWriter fw=new FileWriter(file); BufferedWriter bw=new BufferedWriter(fw); for(Cookie ck:driver.manage().getCookies()) { bw.write(ck.getName()+";"+ck.getValue()+";"+ck.getDomain()+";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure()); bw.newLine(); } bw.flush(); bw.close(); fw.close(); } catch(Exception e) { e.printStackTrace(); } finally { System.out.println("cookie write to file"); } } }
这里,我简单封装了chrome webdriver
import java.util.Arrays; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; public class DriverFactory { public static WebDriver create() { // TODO Auto-generated method stub String chromdriver="E:\\chromedriver.exe"; System.setProperty("webdriver.chrome.driver", chromdriver); ChromeOptions options = new ChromeOptions(); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized")); options.addArguments("--test-type", "--start-maximized"); WebDriver driver=new ChromeDriver(options); return driver; } }
接下来 ,读取browser.data,生成cookie 把cookie加载到浏览器
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Date; import java.util.StringTokenizer; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; public class UseCookieLogin { public static void main(String[] args) { // TODO Auto-generated method stub WebDriver driver=DriverFactory.create(); driver.get("http://selenium.jd-app.com/wp-admin/"); try { File file=new File("broswer.data"); FileReader fr=new FileReader(file); BufferedReader br=new BufferedReader(fr); String line; while((line=br.readLine())!= null) { StringTokenizer str=new StringTokenizer(line,";"); while(str.hasMoreTokens()) { String name=str.nextToken(); String value=str.nextToken(); String domain=str.nextToken(); String path=str.nextToken(); Date expiry=null; String dt; if(!(dt=str.nextToken()).equals(null)) { //expiry=new Date(dt); System.out.println(); } boolean isSecure=new Boolean(str.nextToken()).booleanValue(); Cookie ck=new Cookie(name,value,domain,path,expiry,isSecure); driver.manage().addCookie(ck); } } } catch(Exception e) { e.printStackTrace(); } driver.get("http://selenium.jd-app.com/wp-admin/"); } }
管理页面只有登陆后才能打开,如果未登录就会跳转登陆页面,当我们加载cookie后,看到的是登陆后的页面
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。