博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selenium 使用要点记录<二>
阅读量:6988 次
发布时间:2019-06-27

本文共 2728 字,大约阅读时间需要 9 分钟。

  hot3.png

书接上回,最近项目里边新的release需要move to uat。然后我很光荣的被委派去给tester执行自动化测试脚本做support,让我极度遗憾的是tester不是妹子,表示本丝注定单身孤独终老的命啊。

好吧不扯淡了,在测试的过程中碰到几个问题导致程序不够稳定,脚本也被喷不够robust,我自己也喷page object模式就是shit,维护的人会shi的很难看。

1. 处理popup window问题处理的不够好?

    a. 切换到新弹出的window

        public boolean switchToWindowAttach(WebDriver driver, String windowTitle,String frameId) {		boolean flag = false;		try {		        //记下当前window			String currentHandle = getDriver().getWindowHandle();			Set
 handles = getDriver().getWindowHandles(); for (String s : handles) { if (s.equals(currentHandle)) { continue; } else { driver.switchTo().window(s); if (driver.getTitle().contains(windowTitle)) {     if(!StringUtils.isBlank(frameId)){         //有些window可能要切换到具体的iframe才能操作内部元素         //getDriver().switchTo().defaultContent() 切换回外层 driver.switchTo().frame(frameId); } flag = true; loggerContxt.info("Switch to window: " + windowTitle + " successfully!"); break; } else {         //如果当前循环到的window不是需要切换的window则切换回最初window driver.switchTo().window(currentHandle); continue; } } } } catch (NoSuchWindowException e) { loggerContxt.fatal(String.format("Failed to swith to window whose title contains:: ", windowTitle),e); flag = false; } return flag; }

    b. 关掉处理完成的popup window

/** * close popup window by the title name * @param driver WebDriver * @param title the title of the window need to be closed * @param orginalHandle the Window Handler of current window  * @return */ protected void closePopupByTitle(WebDriver driver, String title,			String orginalHandle) {		for (String handle : driver.getWindowHandles()) {			String theTitle = driver.switchTo().window(handle).getTitle();			//if the title are samilar, then close			if (theTitle.contains(title)) {				driver.close();			}			//switch back to the original window			if (!handle.equalsIgnoreCase(orginalHandle)) {				driver.switchTo().window(orginalHandle);			}		}	}

2. 需要等待页面的某个元素加载完成再做后续操作?

Selenium提供了2个等待的操作,一种是隐式的,另一种,er,也不知道是不是叫现实的

a.

public void waitForElementLoading(Long millis) {	    driver.manage().timeouts().implicitlyWait(millis, TimeUnit.MILLISECONDS);	}

b.

public WebElement waitForElementByLocator(final By locator, Long timeOut) {		if (timeOut == null) {			timeOut = 60L;		}		WebElement id = (new WebDriverWait(getDriver(), timeOut))				.until(new ExpectedCondition
() { @Override public WebElement apply(WebDriver d) { return d.findElement(locator); } }); return id; }

第一个就是隐式的等待啦。第二种我自己隐式的实现了ExceptedCondition,它的apply方法应该会被回调。

ExceptedCondition提供了许多静态的方法大家可以根据自己的需求来使用。

这两种等待的区别和具体使用需要本丝研究一把,下次给大家介绍哈。额,表喷我,这准备不足真心讲不好。

转载于:https://my.oschina.net/athhu/blog/272401

你可能感兴趣的文章
写给MongoDB开发者的50条建议Tip12
查看>>
我的友情链接
查看>>
linux下查看nginx,apache,mysql,php编译命令
查看>>
JQUERY学习第三天之浮动和弹出窗口
查看>>
python中asynchat异步socket命令/响应处理
查看>>
动态编译
查看>>
linux下批量解压缩
查看>>
使用xcopy进行日增量备份
查看>>
知之者不如好之者,好之者不如乐之者
查看>>
测试Application.Idle
查看>>
sizeof与strlen的区别与联系
查看>>
Citrix发布支持Framehawk技术的HDX协议,用户体验优势进一步扩大
查看>>
Android各种访问权限Permission详解
查看>>
RHEL5.5安装中文支持
查看>>
web前端开发中浏览器兼容问题(五)
查看>>
小博老师解析Java核心技术 ——动态解析Jar的运用
查看>>
我的友情链接
查看>>
博为峰Java技术文章 ——JavaSE Swing BoxLayout布局管理器I
查看>>
PC时代的20位英雄
查看>>
经典的MySQL 数据备份daemon程序
查看>>