• WebDriver(C#)之十点使用心得


    使用Selenium WebDriver驱动浏览器测试的过程中多多少少会遇到一些折腾人的问题,总结了一部分,做下分享。

    一、隐藏元素处理(element not visible)

    使用WebDriver点击界面上被隐藏的元素时,使用默认的IWebElement.Click()方法可能无法触发Click事件,这时的修改方案可以采用执行JS的方式来实现。

    IWebElementwebElement = driver.FindElement(By.Id(elementId));

    IJavaScriptExecutorjs = driverasIJavaScriptExecutor;

    js.ExecuteScript("arguments[0].click();",webElement);

    二、页面跳转后找不到元素(no such element)

    页面跳转获取新页面的元素需要时间,所以需要在跳转后增加等待时间,最通用的方法是判断在某个时间内元素是否加载完成。

    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

    三、屏蔽动画

    如果网站使用了JQuery的动画效果,我们在运行测试的时候可以disable JQuery的animation,代码如下:

    IJavaScriptExecutorjs = driverasIJavaScriptExecutor;

    js.ExecuteScript("jQuery.fx.off=true");

    四、不确定出现的元素处理

    有的网站首次访问时会弹出广告,第二次访问则不再显示,这种情况可以自己调用WebDriver的IsElementPresent(Byby)方法进行判断

    if(IsElementPresent(Byby)))

      {

          driver.FindElement(by).Click();

       }

    五、Cookie登录

    自动化测试中,许多地方要求登录,cookie能够实现不必每次输入用户名和密码进行登录。

    Cookiecookie =newCookie(name,value,domain,path,expires);

    driver.Manage().Cookies.AddCookie(cookie);

            说明:参数分别为Cookie的名称,内容,域,路径,过期时间。

    六、图片上传

    参考:《Selenium(C#)实现图片上传的两种方式

    七、带placeholder属性的输入框Clear无效(invalid element state)

    自动测试时,会出现某些带有默认值的输入框Clear()方法报错,错误提示:invalid element state: Element is not currently interactable and may not be manipulated,此时需要检查下输入文本框是否带有placeholder属性,如果有则直接略过Clear方法,原因如下:

    placeholder定义和用法

    placeholder属性提供可描述输入字段预期值的提示信息(hint)。

    该提示会在输入字段为空时显示,并会在字段获得焦点时消失。

    八、切换窗口

    参考《WebDriver(C#)之窗口切换

    foreach(stringwinHandleindriver.WindowHandles)  //遍历当前打开的窗口

       {

           driver.SwitchTo().Window(winHandle);

           if(driver.Title.Contains(title)) //title是新窗口的Title

          {

              break;

           }

        }

    九、Iframe元素定位

    如果一个页面是一个html元素,只有一个head,一个body,使用driver.FindElement()可以查找页面中任何一个元素。但是,页面中如果嵌入<frame…../>是的页面包含多个html元素,这种情况下就先要定位到元素所在的frame,然后再查找对应的元素,代码如下:

    IWebElementframe. = driver.FindElement(By.XPath(".//*[@id='form1']/div[1]/div[1]/iframe"));

    driver.SwitchTo().Frame(frame);

    十、Firefox代*理设置

    WebDriver每次启动一个Firefox的实例时,会生成一个匿名的profile,并不会使用当前Firefox的profile。所以如果要访问需要通过代*理的web服务,直接设置Firefox的代*理是没用的,因为WebDriver启动的Firefox不会使用该profile,需要在代码里设置FirefoxProfile属性,或者直接调用默认的profile。

    publicIWebDriverProxyConfig()

           {

               FirefoxProfilefirefoxProfile =newFirefoxProfile();

               firefoxProfile.SetPreference("network.proxy.type",1);

               firefoxProfile.SetPreference("network.proxy.http","192.168.1.11");

               firefoxProfile.SetPreference("network.proxy.http_port",8888);

               firefoxProfile.SetPreference("network.proxy.no_proxies_on","");

               returnnewFirefoxDriver(firefoxProfile);

           }

    或者启动默认的profile

    stringpath =@"C:UsersusernameAppDataLocalMozillaFirefoxProfilesa8xlln4m.default";

    FirefoxProfileffprofile =newFirefoxProfile(path);

    driver =newFirefoxDriver(ffprofile);

  • 相关阅读:
    凡是可能出错的事必定会出错
    php session 前后台同域名下
    mysql常用; group by 高级
    Thread 学习记录 <1> -- volatile和synchronized
    VBA学习
    EXPLAINING WHAT ACTION AND FUNC ARE
    Unslider.js Tiny Sample
    jQuery操作DOM元素
    在.net中使用GAC
    Sql Server 分区演练 【转】
  • 原文地址:https://www.cnblogs.com/soundcode/p/15094205.html
Copyright © 2020-2023  润新知