摘要:是否加载图片,默认不加载尝试性解决问题关闭失败注意和的区别没打开一个页面就截图最大化禁止下载加载图片尝试性解决问题关闭失败滚动窗口。重新调整窗口大小,以适应页面,需要耗费一定时间。建议等待合理的时间。
import org.openqa.selenium.WebDriver; public interface WebDriverPool { WebDriver get() throws InterruptedException; void returnToPool(WebDriver webDriver); void close(WebDriver webDriver); void shutdown(); }
import java.util.ArrayList; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import net.xby1993.common.util.FileUtil; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author taojw */ public class PhantomjsWebDriverPool implements WebDriverPool{ private Logger logger = LoggerFactory.getLogger(getClass()); private int CAPACITY = 5; private AtomicInteger refCount = new AtomicInteger(0); private static final String DRIVER_PHANTOMJS = "phantomjs"; /** * store webDrivers available */ private BlockingDequeinnerQueue = new LinkedBlockingDeque ( CAPACITY); private String PHANTOMJS_PATH; private DesiredCapabilities caps = DesiredCapabilities.phantomjs(); public PhantomjsWebDriverPool() { this(5,false); } /** * * @param poolsize * @param loadImg 是否加载图片,默认不加载 */ public PhantomjsWebDriverPool(int poolsize,boolean loadImg) { this.CAPACITY = poolsize; innerQueue = new LinkedBlockingDeque (poolsize); PHANTOMJS_PATH = FileUtil.getCommonProp("phantomjs.path"); caps.setJavascriptEnabled(true); caps.setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJS_PATH); // caps.setCapability("takesScreenshot", false); caps.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"); ArrayList cliArgsCap = new ArrayList (); //http://phantomjs.org/api/command-line.html cliArgsCap.add("--web-security=false"); cliArgsCap.add("--ssl-protocol=any"); cliArgsCap.add("--ignore-ssl-errors=true"); if(loadImg){ cliArgsCap.add("--load-images=true"); }else{ cliArgsCap.add("--load-images=false"); } caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap); caps.setCapability( PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, new String[] {"--logLevel=INFO"}); } public WebDriver get() throws InterruptedException { WebDriver poll = innerQueue.poll(); if (poll != null) { return poll; } if (refCount.get() < CAPACITY) { synchronized (innerQueue) { if (refCount.get() < CAPACITY) { WebDriver mDriver = new PhantomJSDriver(caps); // 尝试性解决:https://github.com/ariya/phantomjs/issues/11526问题 mDriver.manage().timeouts() .pageLoadTimeout(60, TimeUnit.SECONDS); // mDriver.manage().window().setSize(new Dimension(1366, // 768)); innerQueue.add(mDriver); refCount.incrementAndGet(); } } } return innerQueue.take(); } public void returnToPool(WebDriver webDriver) { // webDriver.quit(); // webDriver=null; innerQueue.add(webDriver); } public void close(WebDriver webDriver) { refCount.decrementAndGet(); webDriver.quit(); webDriver = null; } public void shutdown() { try { for (WebDriver driver : innerQueue) { close(driver); } innerQueue.clear(); } catch (Exception e) { // e.printStackTrace(); logger.warn("webdriverpool关闭失败",e); } } }
import java.util.HashMap; import java.util.Map; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import net.xby1993.common.util.FileUtil; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ChromeWebDriverPool implements WebDriverPool{ private Logger logger = LoggerFactory.getLogger(getClass()); private int CAPACITY = 5; private AtomicInteger refCount = new AtomicInteger(0); private static final String DRIVER_NAME = "chrome"; /** * store webDrivers available */ private BlockingDequeinnerQueue = new LinkedBlockingDeque ( CAPACITY); private static String DRIVER_PATH; private static DesiredCapabilities caps = DesiredCapabilities.chrome(); static { DRIVER_PATH = FileUtil.getCommonProp("chrome.path"); System.setProperty("webdriver.chrome.driver", FileUtil.getCommonProp("chrome.driver.path")); ChromeOptions options = new ChromeOptions(); // options.addExtensions(new File("/path/to/extension.crx")) // options.setBinary(DRIVER_PATH); //注意chrome和chromeDirver的区别 options.addArguments("test-type"); //ignore certificate errors options.addArguments("headless");// headless mode options.addArguments("disable-gpu"); // options.addArguments("log-path=chromedriver.log"); // options.addArguments("screenshot"); 没打开一个页面就截图 //options.addArguments("start-maximized"); 最大化 //Use custom profile Map prefs = new HashMap (); // prefs.put("profile.default_content_settings.popups", 0); //http://stackoverflow.com/questions/28070315/python-disable-images-in-selenium-google-chromedriver prefs.put("profile.managed_default_content_settings.images",2); //禁止下载加载图片 options.setExperimentalOption("prefs", prefs); caps.setJavascriptEnabled(true); caps.setCapability(ChromeOptions.CAPABILITY, options); // caps.setCapability("takesScreenshot", false); /* Add the WebDriver proxy capability. Proxy proxy = new Proxy(); proxy.setHttpProxy("myhttpproxy:3337"); capabilities.setCapability("proxy", proxy); */ } public ChromeWebDriverPool() { } public ChromeWebDriverPool(int poolsize) { this.CAPACITY = poolsize; innerQueue = new LinkedBlockingDeque (poolsize); } public WebDriver get() throws InterruptedException { WebDriver poll = innerQueue.poll(); if (poll != null) { return poll; } if (refCount.get() < CAPACITY) { synchronized (innerQueue) { if (refCount.get() < CAPACITY) { WebDriver mDriver = new ChromeDriver(caps); // 尝试性解决:https://github.com/ariya/phantomjs/issues/11526问题 mDriver.manage().timeouts() .pageLoadTimeout(60, TimeUnit.SECONDS); // mDriver.manage().window().setSize(new Dimension(1366, // 768)); innerQueue.add(mDriver); refCount.incrementAndGet(); } } } return innerQueue.take(); } public void returnToPool(WebDriver webDriver) { // webDriver.quit(); // webDriver=null; innerQueue.add(webDriver); } public void close(WebDriver webDriver) { refCount.decrementAndGet(); webDriver.quit(); webDriver = null; } public void shutdown() { try { for (WebDriver driver : innerQueue) { close(driver); } innerQueue.clear(); } catch (Exception e) { // e.printStackTrace(); logger.warn("webdriverpool关闭失败", e); } } }
import java.io.Closeable; import java.io.IOException; import org.openqa.selenium.WebDriver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class WebDriverManager implements Closeable{ private static final Logger log=LoggerFactory.getLogger(WebDriverManager.class); private WebDriverPool webDriverPool=null; public WebDriverManager(){ this.webDriverPool=new PhantomjsWebDriverPool(1,false); } public WebDriverManager(WebDriverPool webDriverPool){ this.webDriverPool=webDriverPool; } public void load(String url,int sleepTimeMillis,SeleniumAction... actions){ WebDriver driver=null; try { driver=webDriverPool.get(); driver.get(url); sleep(sleepTimeMillis); WebDriver.Options manage = driver.manage(); manage.window().maximize(); for(SeleniumAction action:actions){ action.execute(driver); } } catch (InterruptedException e) { e.printStackTrace(); log.error("",e); }finally{ if(driver!=null){ webDriverPool.returnToPool(driver); } } } public void load(SeleniumAction... actions){ WebDriver driver=null; try { driver=webDriverPool.get(); WebDriver.Options manage = driver.manage(); manage.window().maximize(); for(SeleniumAction action:actions){ action.execute(driver); } } catch (InterruptedException e) { e.printStackTrace(); log.error("",e); }finally{ if(driver!=null){ webDriverPool.returnToPool(driver); } } } public void shutDown(){ if(webDriverPool!=null){ webDriverPool.shutdown(); } } @Override public void close() throws IOException { shutDown(); } public void sleep(long millis){ try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } }
import org.openqa.selenium.WebDriver; /** * @author taojw * */ public interface SeleniumAction { void execute(WebDriver driver); }
import java.io.File; import java.io.IOException; import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.Dimension; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; /** * @author taojw * */ public class WindowUtil { /** * 滚动窗口。 * @param driver * @param height */ public static void scroll(WebDriver driver,int height){ ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+height+" );"); } /** * 重新调整窗口大小,以适应页面,需要耗费一定时间。建议等待合理的时间。 * @param driver */ public static void loadAll(WebDriver driver){ Dimension od=driver.manage().window().getSize(); int width=driver.manage().window().getSize().width; //尝试性解决:https://github.com/ariya/phantomjs/issues/11526问题 driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); long height=(Long)((JavascriptExecutor)driver).executeScript("return document.body.scrollHeight;"); driver.manage().window().setSize(new Dimension(width, (int)height)); driver.navigate().refresh(); } public static void refresh(WebDriver driver){ driver.navigate().refresh(); } public static void taskScreenShot(WebDriver driver,File saveFile){ if(saveFile.exists()){ saveFile.delete(); } File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(src, saveFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void changeWindow(WebDriver driver){ // 获取当前页面句柄 String handle = driver.getWindowHandle(); // 获取所有页面的句柄,并循环判断不是当前的句柄,就做选取switchTo() for (String handles : driver.getWindowHandles()) { if (handles.equals(handle)) continue; driver.switchTo().window(handles); } } public static void changeWindowTo(WebDriver driver,String handle){ for (String tmp : driver.getWindowHandles()) { if (tmp.equals(handle)){ driver.switchTo().window(handle); break; } } } /** * 打开一个新tab页,返回该tab页的windowhandle * @param driver * @param url * @return */ public static String openNewTab(WebDriver driver,String url){ SetstrSet1=driver.getWindowHandles(); ((JavascriptExecutor)driver).executeScript("window.open(""+url+"","_blank");"); sleep(1000); Set strSet2=driver.getWindowHandles(); for(String tmp:strSet2){ if(!strSet1.contains(tmp)){ return tmp; } } return null; } public static void sleep(long millis){ try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 操作关闭模态窗口 * @param driver * @param type 如Id,ClassName * @param sel 选择器 */ public static void clickModal(WebDriver driver,String type,String sel){ String js="document.getElementsBy"+type+"(""+sel+"")[0].click();"; ((JavascriptExecutor)driver).executeScript(js); } /** * 判断一个元素是否存在 * @param driver * @param by * @return */ public static boolean checkElementExists(WebDriver driver,By by){ try{ driver.findElement(by); return true; }catch(NoSuchElementException e){ return false; } } /** * 点击一个元素 * @param driver * @param by */ public static void clickElement(WebDriver driver,By by){ WebElement tmp=driver.findElement(by); Actions actions=new Actions(driver); actions.moveToElement(tmp).click().perform(); } public static void clickElement(WebDriver driver,WebElement tmp){ Actions actions=new Actions(driver); actions.moveToElement(tmp).click().perform(); } public static Object execJs(WebDriver driver,String js){ return ((JavascriptExecutor)driver).executeScript(js); } public static void clickByJsCssSelector(WebDriver driver,String cssSelector){ String js="document.querySelector(""+cssSelector+"").click();"; ((JavascriptExecutor)driver).executeScript(js); } public static Set getCookies(WebDriver driver){ return driver.manage().getCookies(); } public static void setCookies(WebDriver driver,Set cookies){ if(cookies==null){ return; } //Phantomjs存在Cookie设置bug,只能通过js来设置了。 StringBuilder sb=new StringBuilder(); for(Cookie cookie:cookies){ String js="document.cookie=""+cookie.getName()+"="+cookie.getValue()+";path="+cookie.getPath()+";domain="+cookie.getDomain()+"";"; sb.append(js); } ((JavascriptExecutor)driver).executeScript(sb.toString()); } public static String getHttpCookieString(Set cookies){ String httpCookie=""; int index=0; for(Cookie c:cookies){ index++; if(index==cookies.size()){ httpCookie+=c.getName()+"="+c.getValue(); }else{ httpCookie+=c.getName()+"="+c.getValue()+"; "; } } return httpCookie; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/70044.html
摘要:事实上,自动化测试是相对于手动的。减少人为的错误自动化测试是机器完成,不存在执行过程中人为的疏忽和错误,测试设计完全决定了测试的质量,可以降低减少人为造成的错误。而接口自动化测试,主要是对接口进行测试。 今年6月份,由于经济压力让我下定决心进阶自动化测试,已经24的我做了3年功能测试,坐标广...
摘要:一什么是是一个基于浏览器的自动化工具,她提供了一种跨平台跨浏览器的端到端的自动化解决方案。模块主要用来记录用例执行情况,以便于高效的调查用例失败信息以及追踪用例执行情况。测试用例仓库用例仓库主要用来组织自动化测试用例。 一、什么是Selenium? Selenium是一个基于浏览器的自动化工具,她提供了一种跨平台、跨浏览器的端到端的web自动化解决方案。Selenium主要包括三部分:...
摘要:在这里真心感谢一直在支持我的那几个粉丝,谢谢你们的持续关注点赞。果然,第三个包也是按的步差来的,而为零不变,也不变。函数里面的话就是个循环咯,当条件不满足时就一直加,知道条件满足为止。我每天都会抽时间给我的粉丝解答,给与一些学习资源。 目录 前言 准备工作 分析(x0) 分析(x1) 分析(...
摘要:然而,市面上的测试工具范围太广了,很难做出选择。这篇热门文章将会选出最受欢迎的测试工具并且它已经被更新过以便反映出年的工具状态。是一个根据规范创建的验收测试框架。 为了传播有质量的代码, 我们必须在编码时有测试的观念 (如果不是在做 TDD)。 然而,市面上的PHP测试工具范围太广了,很难做出选择。 这篇热门文章将会选出最受欢迎的测试工具并且它已经被更新过以便反映出2017年的 QA...
摘要:时间永远都过得那么快,一晃从年注册,到现在已经过去了年那些被我藏在收藏夹吃灰的文章,已经太多了,是时候把他们整理一下了。那是因为收藏夹太乱,橡皮擦给设置私密了,不收拾不好看呀。 ...
阅读 2589·2021-11-19 09:56
阅读 836·2021-09-24 10:25
阅读 1585·2021-09-09 09:34
阅读 2154·2021-09-09 09:33
阅读 1011·2019-08-30 15:54
阅读 501·2019-08-29 18:33
阅读 1223·2019-08-29 17:19
阅读 476·2019-08-29 14:19