Selenium WebDriver自动化兼容性项目实施<2>

上一篇 / 下一篇  2014-01-17 21:04:24 / 个人分类:Selenium

二:Selenium+C#自动化脚本开发知识学习

1Selenium中对浏览器的操作

首先生成一个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();

           }

       }

 

 

2Selenium中执行JS     脚本

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

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

 

 

3Selenium中定位页面元素

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 rder = "#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()方法的性能和效率最低下。

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

//清空文本框clear()

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

 

 

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

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

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

 

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

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

Actions action=new Actions(driver)

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

 

7Selenium中点击按钮/链接

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

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

 

 

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

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

//默认等待100

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

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

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

 

9Selenium中模拟鼠标晃动

//模拟光标晃动movebyoffset()

Actions action = new Actions(driver);

action.MoveByOffset(2, 4);

 

 

10Selenium中对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];

               }

           }

 

       }

 

 

 

11Selenium中关闭多个子Browser窗口

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

 

string ldwin = driver.CurrentWindowHandle;

ReadOnlyCollection<string> windows = driver.WindowHandles;

foreach (var win in windows)

{

   if (win != oldwin)

     {

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

 

      }

 

 

}

 

driver.SwitchTo().Window(oldwin);

 

 

12Selenium中对下拉框的操作

//选择下拉框

protected

TAG:

 

评分:0

我来说两句

日历

« 2024-03-23  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 104426
  • 日志数: 53
  • 图片数: 1
  • 建立时间: 2010-10-15
  • 更新时间: 2016-09-27

RSS订阅

Open Toolbar