发布新日志

  • Selenium-Webdriver 自动截图的方法

    2013-12-16 15:51:11

    如下是Webdriver 自动截图的方法, 然后再主程序中或者需要用到截屏的地方调用如下方法(takeScreenShot)即可。

     

        import java.io.File;   

        import java.io.IOException;   

        import java.util.Calendar;   

        import org.openqa.selenium.OutputType;   

        import org.openqa.selenium.TakesScreenshot;   

        import org.openqa.selenium.WebDriver;   

        import com.google.common.io.Files;   

          

        public class ScreenShot {   

               static Calendar ca = Calendar.getInstance();

               static int day=ca.get(Calendar.DATE);//获取日   

               static int minute=ca.get(Calendar.MINUTE);//   

               static int hour=ca.get(Calendar.HOUR);//小时   

               

               public static void takeScreenShot(String name,WebDriver driver) {   

                      String data=day+"_"+hour+"_"+minute;   

                      File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

              

               try {   

                      Files.copy(scrFile, new File("d:\\webdriver\\" +data+name+".jpeg"));

                      System.out.println("截图成功!");     

               }

               

               

                catch (IOException e) {   

                      e.printStackTrace(); 

                   }

              

                }   

        }

  • Selenium-启动Firefox代理的代码

    2013-12-16 14:43:07

    有时候需要设置Firefox的代理,才能访问被测试的网站。
     
     
      String proxyIp = "10.158.140.94";
      int proxyPort = 80;
      FirefoxProfile profile = new FirefoxProfile();
      // 使用代理
      profile.setPreference("network.proxy.type", 1);
      // http协议代理配置
      profile.setPreference("network.proxy.http", proxyIp);  
      profile.setPreference("network.proxy.http_port", proxyPort);
      
      profile.setPreference("network.proxy.ssl", proxyIp);  
      profile.setPreference("network.proxy.ssl_port", proxyPort);
        
      // 所有协议公用一种代理配置,如果单独配置,这项设置为false
      profile.setPreference("network.proxy.share_proxy_settings", true);
        
      // 对于localhost的不用代理,这里必须要配置,否则无法和webdriver通讯
      profile.setPreference("network.proxy.no_proxies_on", "localhost");
        
      // 以代理方式启动firefox
      FirefoxDriver driver  = new FirefoxDriver(profile);
Open Toolbar