selenium 代理 Cookies 截图 等待 调用JS

  1. 改变用户代理
  2. 读取Cookies 
  3. 调用Java Script
  4. Webdriver截图
  5. 页面等待

1. 改变用户代理

[java] view plain copy
 
  1. import org.junit.AfterClass;  
  2. import org.junit.BeforeClass;  
  3. import org.junit.Test;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6. import org.openqa.selenium.firefox.FirefoxProfile;  
  7.   
  8. public class ProxyTest {  
  9.     static WebDriver driver;  
  10.   
  11.     @BeforeClass  
  12.     public static void beforeClass(){  
  13.         //代理的IP和端口  
  14.         String proxyIP="192.168.12.0";  
  15.         int proxyPort=80;  
  16.         FirefoxProfile profile=new FirefoxProfile();  
  17.         //使用代理  
  18.         profile.setPreference("network.proxy.type", 1);  
  19.         //配置“HTTP代理:(N)”  
  20.         profile.setPreference("network.proxy.http", proxyIP);  
  21.         profile.setPreference("network.proxy.http_prot", proxyPort);  
  22.         //选中 “为所有协议使用相同代理(S)” 选框  
  23.         profile.setPreference("network.proxy.share_proxy_settings", true);  
  24.         //配置“不使用代理:(N)”文本框  
  25.         profile.setPreference("network.proxy.no_proxies_on", "localhost, 127.0.0.1");  
  26.         //以代理的方式启动火狐  
  27.         driver=new FirefoxDriver(profile);  
  28.     }  
  29.       
  30.     @Test  
  31.     public void test() {  
  32.         driver.get("http://www.baidu.com");  
  33.     }  
  34.       
  35.     @AfterClass  
  36.     public static void afterClass(){  
  37.         //driver.quit();  
  38.     }  
  39. }  

运行之后,在打开的火狐上查看代理设置(选项->高级->网络->连接->设置),显示如下:

友情提示:测试完之后一定要改回来。默认是“使用系统代理设置”。

2.读取Cookies 

增加cookie:

Cookie cookie = new Cookie("key", "value");

driver.manage().addCookie(cookie);

获取cookie的值:

Set<Cookie> allCookies = driver.manage().getCookies();

根据某个cookie的name获取cookie的值:

driver.manage().getCookieNamed("cookieName");

删除cookie:  

driver.manage().deleteCookieNamed("CookieName");

driver.manage().deleteCookie(loadedCookie);

driver.manage().deleteAllCookies();

下面是一个例子:

[java] view plain copy
 
  1. import java.util.Set;  
  2. import org.junit.After;  
  3. import org.junit.Before;  
  4. import org.junit.Test;  
  5. import org.openqa.selenium.Cookie;  
  6. import org.openqa.selenium.WebDriver;  
  7. import org.openqa.selenium.firefox.FirefoxDriver;  
  8.   
  9. public class CookieTest {  
  10.     WebDriver driver;  
  11.       
  12.     @Before  
  13.     public void setUp() throws Exception {  
  14.         driver=new FirefoxDriver();  
  15.     }  
  16.   
  17.     @After  
  18.     public void tearDown() throws Exception {  
  19.         driver.quit();  
  20.     }  
  21.   
  22.     @Test  
  23.     public void test() {  
  24.         printCookie();  
  25.         //添加一个cookie  
  26.         Cookie cookie=new Cookie("s","selenium");  
  27.         driver.manage().addCookie(cookie);  
  28.         printCookie();  
  29.         //删除一个cookie  
  30.         driver.manage().deleteCookie(cookie);   //driver.manage().deleteCookieNamed("s");也可以  
  31.         printCookie();  
  32.     }  
  33.       
  34.     public void printCookie(){  
  35.         //获取并打印所有的cookie  
  36.         Set<Cookie> allCookies=driver.manage().getCookies();  
  37.           
  38.         System.out.println("----------Begin---------------");  
  39.         for(Cookie c:allCookies){  
  40.             System.out.println(String.format("%s->%s",c.getName(),c.getValue()));  
  41.         }  
  42.         System.out.println("----------End---------------");  
  43.     }  
  44. }  

运行结果显示为:

----------Begin---------------
----------End---------------
----------Begin---------------
s->selenium
----------End---------------
----------Begin---------------
----------End---------------
可以看到添加cookie之前,系统的cookie为空。之后成功添加了cookie(s,selenium)并且最后成功删除。

3. 调用Java Script

selenium提供了executeScriptexecuteAsyncScript两个方法来处理js调用的问题。其中executeScript是同步方法,用它执行js代码会阻塞主线程执行,直到js代码执行完毕;executeAsyncScript方法是异步方法,它不会阻塞主线程执行。

[java] view plain copy
 
  1. import org.junit.Test;  
  2. import org.openqa.selenium.WebDriver;  
  3. import org.openqa.selenium.firefox.FirefoxDriver;  
  4. import org.openqa.selenium.JavascriptExecutor;  
  5.   
  6. public class JSTest {  
  7.     @Test  
  8.     public void test() {  
  9.         WebDriver driver=new FirefoxDriver();  
  10.         driver.get("http://www.baidu.com");  
  11.         JavascriptExecutor js=(JavascriptExecutor)driver;  
  12.         js.executeScript("document.getElementById("kw").value="selenium"");  
  13.         //利用js代码获取百度搜索框内文字  
  14.         String keyword = (String) (js.executeScript("var input = document.getElementById("kw").value;return input"));  
  15.         System.out.println(keyword);  
  16.         driver.quit();  
  17.     }  
  18. }  

运行结果:

selenium

4. Webdriver截图

[java] view plain copy
 
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import org.apache.commons.io.FileUtils;  
  4. import org.junit.Test;  
  5. import org.openqa.selenium.OutputType;  
  6. import org.openqa.selenium.TakesScreenshot;  
  7. import org.openqa.selenium.WebDriver;  
  8. import org.openqa.selenium.firefox.FirefoxDriver;  
  9.   
  10. public class ScreenShotTest {  
  11.   
  12.     @Test  
  13.     public void test() {  
  14.         WebDriver driver=new FirefoxDriver();  
  15.         driver.get("http://www.baidu.com");  
  16.         screenShot("selenium.jpg", (TakesScreenshot)driver);  
  17.         driver.quit();  
  18.     }  
  19.       
  20.     public static void screenShot(String name,TakesScreenshot driver){  
  21.         String savaPath=System.getProperty("user.dir");  
  22.         File sourceFile=driver.getScreenshotAs(OutputType.FILE);  
  23.         try{  
  24.             System.out.println("Begin saving screenshot to path:"+savaPath);  
  25.             FileUtils.copyFile(sourceFile, new File(savaPath+"\"+name));  
  26.         } catch(IOException e){  
  27.             System.out.println("Save screenshot failed!");  
  28.             e.printStackTrace();  
  29.         } finally{  
  30.             System.out.println("Finish screenshot!");  
  31.         }  
  32.     }  
  33. }  

运行结果:

Begin saving screenshot to path:D:workspace_lunaSeleniumDemo
Finish screenshot!

由于eclipse的workspace不一样,会导致上面的路径不一样。不过没关系,我们打开对应的路径就可以看到selenium.jpg。打开以后是百度首页的截图。

5. 页面等待

因为Load页面需要一段时间,如果页面还没加载完就查找元素,必然是查找不到的。最好的方式,就是设置一个默认等待时间,在查找页面元素的时候如果找不到就等待一段时间再找,直到超时。Webdriver提供两种方法,一种是显性等待,另一种是隐性等待。

显性等待:

显示等待就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.

[java] view plain copy
 
  1. new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("kw")));  

当然也可以自己重写内部类:

[java] view plain copy
 
  1. WebElement e = (new WebDriverWait( driver, 10)).until(  
  2.         new ExpectedCondition< WebElement>(){  
  3.             @Override  
  4.             public WebElement apply( WebDriver d) {  
  5.                 return d.findElement(By.linkText("Selenium_百度百科"));  
  6.             }  
  7.         });  

下面的例子是:打开百度,搜索“selenium",等待搜索结果里出现“Selenium_百度百科”的时候点击该链接。

[java] view plain copy
 
  1. import org.junit.Test;  
  2. import org.openqa.selenium.By;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6. import org.openqa.selenium.support.ui.ExpectedCondition;  
  7. import org.openqa.selenium.support.ui.ExpectedConditions;  
  8. import org.openqa.selenium.support.ui.WebDriverWait;  
  9.   
  10. public class ExplicitWaitTest {  
  11.     @Test  
  12.     public void test() {  
  13.         WebDriver driver=new FirefoxDriver();  
  14.         driver.get("http://www.baidu.com");  
  15.         driver.findElement(By.id("kw")).sendKeys("selenium");  
  16.         driver.findElement(By.id("su")).click();  
  17.         //方法一  
  18.         //new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(By.linkText("Selenium_百度百科")));  
  19.         //driver.findElement(By.linkText("Selenium_百度百科")).click();  
  20.         //方法二  
  21.         WebElement e = (new WebDriverWait( driver, 10)).until(  
  22.                 new ExpectedCondition< WebElement>(){  
  23.                     @Override  
  24.                     public WebElement apply( WebDriver d) {  
  25.                         return d.findElement(By.linkText("Selenium_百度百科"));  
  26.                     }  
  27.                 }  
  28.             );  
  29.         e.click();  
  30.           
  31.         driver.quit();  
  32.     }  
  33. }  

运行的话是通过的,但是如果中间不显示等待的话,直接查找就会fail。

隐性等待:

隐形等待只是等待一段时间,元素不一定要出现,只要时间到了就会继续执行。

[java] view plain copy
 
  1. driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);  

还是上面的例子,用隐性等待写就是:

[java] view plain copy
 
  1. import java.util.concurrent.TimeUnit;  
  2. import org.junit.Test;  
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6.   
  7. public class ImplicitWaitTest {  
  8.     @Test  
  9.     public void test() {  
  10.         WebDriver driver=new FirefoxDriver();  
  11.         driver.get("http://www.baidu.com");  
  12.         driver.findElement(By.id("kw")).sendKeys("selenium");  
  13.         driver.findElement(By.id("su")).click();  
  14.         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);  
  15.         driver.findElement(By.linkText("Selenium_百度百科")).click();  
  16.           
  17.         driver.quit();  
  18.     }  
  19. }  

运行结果还是通过的。不过个人觉得看起来跟笨方法”Thread.sleep()"差不多,只不多比它更高效了而已。

原文地址:https://www.cnblogs.com/111testing/p/6986914.html