使用Selenium进行web自动化测试

之前一直是用selenium进行web自动化测试的,把之前用selenium写用例的过程中需要注意的地方和问题点和大家一起分享一下

1、Selenium简介

Selenium Selenium 是ThoughtWorks专门为Web应用程序编写的一个验收测试工具。

Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE、Google Chrome、Mozilla Firefox、Mozilla Suite等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建衰退测试检验软件功能和用户需求。支持自动录制动作和自动生成。Net、Java、Perl等不同语言的测试脚本。

2、Selenium+Visual Studio环境配置

先到官网先下载IEDriverServer(32位或者64位),解压文件将IEDriverServer.exe存放到一个指定路径,要记住这个路径,配置过程中要用到。(http://docs.seleniumhq.org/download/ )

如果使用chrome,对应的驱动文件是chromedriver.exe,也是需要把chromedriver.exe存放到指定路径。(http://chromedriver.storage.googleapis.com/index.html)

如果是火狐浏览器,对应的驱动文件是geckodriver.exe,同样也是需要把geckodriver.exe存放到指定路径。(http://tv.cctv.com/live/)

打开VS(为了便于查看,我用的是中文版的,英文版的请自己对照位置),打开“工具”菜单下的“扩展管理器”:

我们需要在“扩展管理器”中安装“NuGet”,单击下载安装,然后重启你的VS。

创建一个测试项进行测试

右键“引用”,选择“NuGet程序包”。

在这里选择“联机”,搜索“selenium”。然后安装就行了。

安装Selenium包后项目引用里可看到如下动态库

创建一个测试类进行环境验证(注意到“C:\IEDriverServer”了吗?这就是存放IEDriverServer.exe的路径)

可能遇到的问题:

1):未找到驱动服务

参考方案: 就看看IEDriverServer.exe的路径是不是错了。 另外:浏览器的“启动保护模式”我给取消勾选了,如果勾选的话偶尔也会报异常。

2):用户代码未处理

参考方案: 看看是不是浏览器的驱动用错了(32位用了64位的,或者反过来)。

3、Selenium—页面元素定位

By.className(className))

By.cssSelector(selector)

By.id(id)

By.linkText(linkText)

By.name(name)

By.partialLinkText(linkText)

By.tagName(name)

By.xpath(xpathExpression)

注意:

selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。

By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条 件的元素List,如果不存在符合条件的就返回一个空的list。

各方法使用优先级为:id,name,linktext优先使用,cssselector次之,最后使用xpath。

4、Selenium自动化--对象操作

1):点击按钮/链接click()

Driver.FindElement(By.XPath(“//input[@id=‘submit’ and @value=‘下一步’]”)).click();

2):清空文本框clear()

Driver.FindElement(By.Id(“tranAmtText”)).clear();

3):在文本框中输入指定的字符串sendkeys()

Driver.FindElement(By.Id("tranAmtText")).SendKeys(“123456”);

4):移动光标到指定的元素上perform

Actions action=new Actions(driver);

action.MoveToElement(Find(By.XPath("//input[@id='submit' and @value='确定']"))).Perform();

5):模拟光标晃动movebyoffset()

Actions action = new Actions(driver);

action.MoveByOffset(2, 4);

6):等待页面元素加载完成,默认等待100秒

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));

//等待页面上ID属性值为submitButton的元素加载完成

wait.Until((d) => { return WaitForObject(By.Id("submitButton")); });

5、Selenium自动化—操作下拉框

Select操作//选择下拉框

protected void SelectUsage(string selectid, string text) {

IWebElement select = Find(By.Id(selectid));

IList<IWebElement> AllOptions = select.FindElements(By.TagName("option"));

foreach (IWebElement option in select.FindElements(By.TagName("option"))){

if (option.GetAttribute("value").Equals(text))

option.Click();

}

}

6、Selenium自动化—操作iframe

1):切换焦点到id为固定值的iframe上

进入页面后,光标默认焦点在DefaultContent中,若想要定位到iframe 需要转换焦点

driver.SwitchTo().DefaultContent();

//切换焦点到mainFrame

driver.SwitchTo().Frame("mainFrame");

需要注意的是:切换焦点之后若想切换焦点到其他iframe上 需要先返回到defaultcontent,再切换焦点到指定的iframe上。

2):切换焦点到id值为动态值的iframe上

有时候 页面上浮出层的id为动态值,此时需要先获取所有符合记录的iframe放置在数组中,然后遍历数组切换焦点到目标iframe上。 如下方法:

protected string bizFrameId = string.Empty;

protected string bizId = string.Empty;

//获取动态iframe的id值

protected void SetIframeId() {

ReadOnlyCollection<IWebElement> els = driver.FindElements(By.TagName("iframe"));

foreach (var e in driver.FindElements(By.TagName("iframe"))) {

string s1 = e.GetAttribute("id");

if (s1.IndexOf("window") >= 0 && s1.IndexOf("content") >= 0) {

bizFrameId = e.GetAttribute("id");

string[] ss = s1.Split(new char[] { '_' });

bizId = ss[1];

}

}

}

7、Selenium自动化—alert prompt confirm

//在本次浏览器兼容性测试项目中遇到的只有confirm和alert

//下面举例说明confirm和alert的代码,prompt类似

//confirm的操作

IAlert confirm = driver.SwitchTo().Alert();

confirm.Accept();

//Alert的操作

//健康直播间中同样的业务有时候不会弹对alert,此时需要判断alert是否存在

//对Alert提示框作确定操作,默认等待50毫秒

protected void AlertAccept() {

AlertAccept(0.05);

}

//等待几秒,可以为小数,单位为秒

protected void AlertAccept(double waitseSonds) {

double nsleepMillon = waitseSonds * 1000;

int k=0; int split=50; IAlert alert = null;

do {

k++;

Thread.Sleep(split);

alert = driver.SwitchTo().Alert();

} while (k * split <= nsleepMillon || alert==null);

if (alert != null) {

alert.Accept();

}

}

8、Selenium自动化—浏览器操作

首先生成一个Web对象

WebDriver driver = new FirefoxDriver();

//打开指定的URL地址

driver.Navigate().GoToUrl(@"http://doctor.guahao.com");

//关闭浏览器

Driver.quit();

//获取所有的WindowHandle,关闭所有子窗口

string oldwin = driver.CurrentWindowHandle;

ReadOnlyCollection<string> windows = driver.WindowHandles;

foreach (var win in windows) {

if (win != oldwin) {

driver.SwitchTo().Window(win).Close();

}

}

driver.SwitchTo().Window(oldwin);

如果有需要改进的地方需要大家多多指点,一起学习共同进步!!!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章