自动化测试:Selenium webdriver 学习笔记-C#版(四)

  前面我们知道了如何进行对象的定位,下面我们进一步来了解selenium的一些功能特性:

 

1>等待:我们在处理对象的时候,对象并不能及时的“出现”,那么此时我们就需要进行等待了。

 driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20)); //这里的20,是以"s" 为单位,这里的数值可以根据实际情况来设置,

  

     还有一种等待方式:使用WebDriverWait对象,同样强大。

public IWebElement WaitForElement(IWebDriver driver, string el_id,int timeout)   //找到元素就返回
        {
            IWebElement ele = null;
            try
            {
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
                 ele = wait.Until<IWebElement>((d) =>
                {
                    return d.FindElement(By.Id(el_id));
                });               
            }
            catch
            {
                Console.WriteLine("12e");
           }
            return ele;
        }

 

 

2>获取窗体句柄:

driver.CurrentWindowHandle

 

3>窗体转换:

driver.SwitchTo().Window();

 

4>使用Actions

var xx = driver.FindElement(By.Id("id"));
Actions builder = new Actions(driver);
builder.MoveToElement(xx).Perform();

 

5>根据链接文本定位对象(不太适用本地化测试)

driver.FindElement(By.PartialLinkText("登录"));

 

6>处理弹出框

driver.SwitchTo().Alert().Accept()//简单讲就是点击yes
driver.SwitchTo().Alert().Dismiss()//点击No

 

 7>判断页面字符串

driver.PageSource.Contains("any word;");

 

 

 

下面来看具体的实例:

登陆百度首页->搜索“博客园”->登陆博客园->退出->处理弹出框

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
//添加selenium的引用
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;

using OpenQA.Selenium.Interactions;
//添加引用-在程序集中添加System.Drawing
using System.Drawing;
using System.Drawing.Imaging;

namespace Selenium
{
    class Program
    {
        static void Main(string[] args)
        {
            //此时记得添加路径    
            using (var driver = new InternetExplorerDriver(@"D:\Selenium\IEDriverServer_x64_2.34.0\"))
            {

                //进入百度首页
                driver.Navigate().GoToUrl(@"http://www.baidu.com");
                Thread.Sleep(1000);
//是否包含"百度"这个字符串,可以用来判断页面是否出现
if (driver.PageSource.Contains("百度"))
{ Console.WriteLine(" 123");

}
//设置窗体最大化
                driver.Manage().Window.Maximize();
                Thread.Sleep(1000);

                //找到对象     
                var colSearchBox = driver.FindElementsByName("wd");
                var btnClick = driver.FindElement(By.Id("su1"));

                //发送搜索内容
                colSearchBox[1].SendKeys("bokeyuan");
               //Thread.Sleep(1000);

                //等待搜索结果
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                IWebElement searchResutl = null;
                try
                {
                     searchResutl = wait.Until<IWebElement>((d) =>
                    {
                        return d.FindElement(By.ClassName("bdsug"));

                    });
                }
                catch(Exception e)
                {
                    Console.WriteLine("Timeout to find element:" + " "+ e.Message.ToString());
                }

                //搜索结果数量
                var searchResult_Children = searchResutl.FindElements(By.TagName("li"));
               
                foreach (IWebElement child in searchResult_Children)
                {
                    if (child.Text.Equals("博客园"))
                    {    
                        //选择正确的搜索对象
                        child.Click();
                        break;
                    }
                }

                //设置页面加载时间
                driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));

                //获取当前页面句柄 ,适用于一个窗体
                //var cc = driver.CurrentWindowHandle;

                //进入首页
                var homePage = driver.FindElement(By.ClassName("result"));
                var homePage_child = homePage.FindElement(By.Id("1"));
                homePage_child.FindElement(By.ClassName("favurl")).Click();

                //设置页面加载时间
                driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));

                //获取当前网页的句柄,使用与多个窗体
                //那么我们需要的是第二个窗体
                var currentWindowHandle = driver.WindowHandles[1];

                //因为要现在要处理的对象在新窗体上,所以这里要进行窗体转换
                driver.SwitchTo().Window(currentWindowHandle);
                
                //下面将鼠标移动到左边的".Net技术",此时会滑出相应的模块
                //用XPath定位对象,此处找到"新手区"

                //移动鼠标
                var xx = driver.FindElement(By.Id("cate_item_108698"));
                Actions builder = new Actions(driver);
                builder.MoveToElement(xx).Perform();

                Thread.Sleep(2000);

                //使用XPath 找对象
                //driver.FindElement(By.Id("cate_sub_block")).FindElement(By.XPath("//div[1]/div[2]/ul[1]/li[1]/a[@href='/cate/beginner/']")).Click();
                //div[1]第一个div
                //上面的比较"笨重",下面的简单多了
                driver.FindElement(By.Id("cate_sub_block")).FindElement(By.XPath("//a[@href='/cate/beginner/']")).Click();

                //登陆

                //使用PartialLinkText定位对象
                var btnLogin1 = driver.FindElement(By.PartialLinkText("登录"));
                btnLogin1.Click();
                var txtUserName = driver.FindElement(By.Id("tbUserName"));
                txtUserName.SendKeys("Alvin-x");
            
                var txtPassword = driver.FindElement(By.Id("tbPassword"));
                txtPassword.SendKeys("123456");

                var btnLogin2 = driver.FindElement(By.Id("btnLogin"));
                btnLogin2.Click();
                Thread.Sleep(2000);

                //使用CssSelector定位对象
                //点击“退出”
                var btnBackup = driver.FindElement(By.CssSelector("a[href='#']"));
                btnBackup.Click();

                //等待弹出框弹出后再处理它 
                Thread.Sleep(1000);
                IAlert result = null;
                while (1 < 2)
                {
                    try
                    {
                        result = driver.SwitchTo().Alert();
                    }
                    catch (Exception)
                    {
                        result = null;
                    }

                    if (result != null)
                    {
                        result.Accept();
                        break;
                    }
                }

                //退出
                driver.Quit();
                
            }
        }
    }
}

 

 

 

   

 

posted on 2014-02-24 17:01  Alvin-x  阅读(6158)  评论(1编辑  收藏  举报

导航