51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1263|回复: 1
打印 上一主题 下一主题

[转贴] 使用Selenium进行web自动化测试

[复制链接]
  • TA的每日心情
    无聊
    16 分钟前
  • 签到天数: 943 天

    连续签到: 2 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-1-12 13:37:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    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));
    IListAllOptions = 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();
    //切换焦点到
    mainFramedriver.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() {
    ReadOnlyCollectionels = 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 ldwin = driver.CurrentWindowHandle;
    ReadOnlyCollectionwindows = driver.WindowHandles;
    foreach (var win in windows) {
    if (win != oldwin) {
    driver.SwitchTo().Window(win).Close();
    }
    }
    driver.SwitchTo().Window(oldwin);


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏1
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-7 11:57 , Processed in 0.075828 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表