转一篇文章,有修改,出处http://www.7dtest.com/site/blog-2880-203.html

1:Selenium中对浏览器的操作

首先生成一个Web对象

IWebDriver driver = new FirefoxDriver();

//打开指定的URL地址

driver.Navigate().GoToUrl(@"http://12.99.102.196:9080/corporbank/logon_pro.html");

//关闭浏览器

Driver.quit();

网银浏览器兼容性测试过程中,关闭浏览器后会有对话框,此问题解决方法如下:

public void logout()

{

System.Diagnostics.Process[] myProcesses;

myProcesses = System.Diagnostics.Process.GetProcessesByName("IEXPLORE");

foreach (System.Diagnostics.Process instance in myProcesses)

{

instance.Kill();

}

}

2:Selenium中执行JS      脚本

//需要将driver强制转换成JS执行器类型

((IJavaScriptExecutor) driver).ExecuteScript("js文件名")

3:Selenium中定位页面元素

driver.FindElement(By.Id("cp1_btnModify")).click();

By.ClassName(className));     
    By.CssSelector(selector) ;       
    By.Id(id);                      
    By.LinkText(linkText);           
    By.Name(name);              
    By.PartialLinkText(linkText); 
    By.TagName(name);        
    By.Xpath(xpathExpression);

3.1根据元素id定位并操作

//向指定的文本框输入字符串500001

Driver.FindElement(By.Id("amount")).SendKeys("500001");

3.2根据元素classname定位并操作

//点击classname为指定值的按钮

Driver.FindElement(By.ClassName(“WotherDay”)).click()

3.3根据元素的linktext定位并操作

Driver.FindElement(By.LinkText(“选择账号”)).click()

3.4根据元素的Name定位并操作

Driver.FindElement(By.Name(“quality”)).perform()

 

3.5使用CssSelector定位并操作

string order = "#resultTable.result_table tbody tr.bg1 td.center a";

driver.FindElement (By.CssSelector(order)).click()

3.6使用Xpath定位并元素并操作

//使用多个属性定位元素

Driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click()

//使用绝对路径定位元素

string path = "/html/body/div[4]/div/div/div[2]/table/tbody/tr/td/a";

Driver.FindElement(By.Xpath(path)).click()

各方法使用优先原则:

优先使用id,name,classname,link;次之使用CssSelector();最后使用Xpath();

因为Xpath()方法的性能和效率最低下。

4:Selenium中清空文本框中的默认内容

//清空文本框clear()

Driver.FindElement(By.Id("tranAmtText")).clear()

5:Selenium中在指定的文本框中输入指定的字符串

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

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

6:Selenium中移动光标到指定的元素上

//移动光标到指定的元素上perform

Actions action=new Actions(driver)

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

7:Selenium中点击按钮/链接

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

Driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click()

 

 

8:Selenium中等待页面上的元素加载完成

//等待页面元素加载完成

//默认等待100秒

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

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

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

 

9:Selenium中模拟鼠标晃动

//模拟光标晃动movebyoffset()

Actions action = new Actions(driver);

action.MoveByOffset(2, 4);

10:Selenium中对iframe中元素的定位

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

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

driver.SwitchTo().DefaultContent();

//切换焦点到mainFrame

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

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

 

 

5.2切换焦点到id值为动态值的iframe上

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

如下方法:

protected string bizFrameId = string.Empty;

protected string bizId = string.Empty;

//获取动态iframeid

        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];

                }

            }

 

        }

 

 

 

11:Selenium中关闭多个子Browser窗口

//获取所有的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);

12:Selenium中对下拉框的操作

//选择下拉框

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();

}

}

13:Selenium中对confirm ,alert ,prompt的操作

//在本次浏览器兼容性测试项目中遇到的只有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();

            }

  }

14:Selenium WebDriver的截图功能

//WebDriver中自带截图功能

Screenshot screenShotFile = ((ITakesScreenshot)driver).GetScreenshot(); 
screenShotFile.SaveAsFile("test",ImageFormat.Jpeg);

PS:文中没提到Page Object Design Model,之后补上。

Selenium Webdriver 自动化测试开发常见问题(C#版)的更多相关文章

  1. selenium webdriver自动化测试

    selenium家族介绍           Selenium IDE:Selenium IDE是嵌入到Firefox浏览器中的一个插件,实现简单的浏览器操作的录制与回放功能.   Selenium ...

  2. python + selenium webdriver 自动化测试 之 环境异常处理 (持续更新)

    1.webdriver版本与浏览器版本不匹配,在执行的时候会抛出如下错误提示 selenium.common.exceptions.WebDriverException: Message: unkno ...

  3. Selenium & Webdriver 远程测试和多线程并发测试

    Selenium & Webdriver 远程测试和多线程并发测试 Selenium Webdriver自动化测试,初学者可以使用selenium ide录制脚本,然后生成java程序导入ec ...

  4. selenium webdriver (python) 第二版

    前言 对于大多软件测试人员来讲缺乏编程经验(指项目开发经验,大学的C 语言算很基础的编程知识)一直是难以逾越的鸿沟,并不是说测试比开发人员智商低,是国内的大多测试岗位是功能测试为主,在工作时间中,我们 ...

  5. selenium webdriver (python) 第三版

    感谢 感谢购买第二版的同学,谢谢你们对本人劳动成果的支持!也正是你们时常问我还出不出第三版了,也是你们的鼓励,让我继续学习整理本文档. 感谢乙醇前辈,第二版的文档是放在他的淘宝网站上卖的,感谢他的帮忙 ...

  6. Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通

    Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通Selenium接口性能自动化测试基础部分:分层自动化思想Slenium介绍Selenium1.0/2.0/3.0Slenium R ...

  7. 转载 基于Selenium WebDriver的Web应用自动化测试

    转载原地址:  https://www.ibm.com/developerworks/cn/web/1306_chenlei_webdriver/ 对于 Web 应用,软件测试人员在日常的测试工作中, ...

  8. 【自动化测试&爬虫系列】Selenium Webdriver

    文章来源:公众号-智能化IT系统. 一. Selenium Webdriver技术介绍 1. 简介 selenium Webdriver是一套针对不同浏览器而开发的web应用自动化测试代码库.使用这套 ...

  9. Selenium WebDriver屏幕截图(C#版)

    Selenium WebDriver屏幕截图(C#版)http://www.automationqa.com/forum.php?mod=viewthread&tid=3595&fro ...

随机推荐

  1. [DeploymentService:290066]Error occurred while downloading files from admin server for deployment request "0". Underlying error is: "null"

    weblogic 莫名无法启动: <Apr , :: PM CST> <Error> <Deployer> <BEA-> <Failed to i ...

  2. python urlopen

    Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据. urlopen返回 一个类文件对象(fd),它提供了如下方法:read() , re ...

  3. 20165305 苏振龙《Java程序设计》第五周学习总结

    第七章 Java支持在一个类中声明另一个类,这样的类称作内部类,而包含内部类的类成为内部类的外嵌类. 和某类有关的匿名类就是该类的一个子类,该子类没有明显的用类声明来定义,所以称做匿名类. 和某接口有 ...

  4. create-react-app @observer装饰器报错

    npm install --save-dev babel-plugin-transform-decorators-legacy 然后在node_modules/babel-preset-react-a ...

  5. sqlyog连接Linux上的mysql报错误号码2013,错误号码1130的解决办法

    sqlyog连接Linux上的mysql报错误号码2013,错误号码1130的解决办法 1.报错误号码2013,可能是端口号不是默认的3306,需要改成对应的,检查命令是: [root@host et ...

  6. golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍

    golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍 go语言爬虫框架:gocolly/colly,goquery,colly,chrom ...

  7. 远程服务调用RPC框架介绍,微服务架构介绍和RPC框架对比,dubbo、SpringClound对比

    远程服务调用RPC框架介绍,微服务架构介绍和RPC框架对比,dubbo.SpringClound对比 远程服务调用RPC框架介绍,RPC简单的来说就是像调用本地服务一样调用远程服务. 分布式RPC需要 ...

  8. [转载]SQL Server中的事务与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

  9. SpringMVC配置字符编码过滤器CharacterEncodingFilter来解决表单乱码问题

    1.GET请求 针对GET请求,可以配置服务器Tomcat的conf\server.xml文件,在其第一个<Connector>标签中,添加URIEncoding="UTF-8& ...

  10. pyqt5 点开小窗口

    # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * class Fi ...