• Selenium


    今天突然想了解下Selenium。虽然很久以前,有特意学过Selenium,也用C#写入测试样例,但由于那时什么都没有记录下来,之前积累的经验为0了。以后对自己做过的项目,学过的技术,过一段时间后,要进行总结。

    自己写的 StartSelenium.bat 脚本

    java -jar "D:Selemiumselenium-server-standalone-2.28.0.jar" -trustAllSSLCertificates


    自己写的测试样例:

    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading;
    using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.Support.UI;
    
    namespace SeleniumTests
    {
        [TestFixture]
        public class Login
        {
            private IWebDriver driver;
            private StringBuilder verificationErrors;
            private string baseURL;
            private bool acceptNextAlert = true;
            
            [SetUp]
            public void SetupTest()
            {
                driver = new FirefoxDriver();
                baseURL = "https://*/login.php";
                verificationErrors = new StringBuilder();
            }
            
            [TearDown]
            public void TeardownTest()
            {
                try
                {
                    driver.Quit();
                }
                catch (Exception)
                {
                    // Ignore errors if unable to close the browser
                }
                Assert.AreEqual("", verificationErrors.ToString());
            }
            
            [Test]
            public void TheLoginTest()
            {
                driver.Navigate().GoToUrl(baseURL + "/login.php?action=logout");
                driver.FindElement(By.Id("username")).Clear();
                driver.FindElement(By.Id("username")).SendKeys("***");
                driver.FindElement(By.Id("password")).Clear();
                driver.FindElement(By.Id("password")).SendKeys("***");
                driver.FindElement(By.Name("Per_sel_bu1")).Click();
                driver.FindElement(By.LinkText("IDC管理")).Click();
                // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | if_sidebar | ]]
                driver.FindElement(By.LinkText("排班管理")).Click();
                driver.FindElement(By.LinkText("设备管理")).Click();
            }
            private bool IsElementPresent(By by)
            {
                try
                {
                    driver.FindElement(by);
                    return true;
                }
                catch (NoSuchElementException)
                {
                    return false;
                }
            }
            
            private string CloseAlertAndGetItsText() {
                try {
                    IAlert alert = driver.SwitchTo().Alert();
                    if (acceptNextAlert) {
                        alert.Accept();
                    } else {
                        alert.Dismiss();
                    }
                    return alert.Text;
                } finally {
                    acceptNextAlert = true;
                }
            }
        }
    }

    看到里面有个方法:CloseAlertAndGetItsText,应该是关闭浏览的 alert ,并获取 alert 里面的文本。虽然Greasemonkey也可以实现部分Selenium的功能,但从这个函数看来,Selenium比Greasemonkey强多了。

    Selenium IDE 比较简单,能够录制视频。

  • 相关阅读:
    Windows / Linux / MacOS 设置代理上网的方法汇总
    Centos7 配置 sendmail、postfix 端口号25、465
    CentOS 6/7 配置 sendEmail 发送邮件
    Python 发送 email 的三种方式
    Linux curl 命令模拟 POST/GET 请求
    Python + Selenium + Chrome 使用代理 auth 的用户名密码授权
    Python + Selenium + Firefox 使用代理 auth 的用户名密码授权
    Jenkins+JMeter+Ant 接口持续集成
    接口自动化、移动端、web端自动化如何做?
    pytest--命令行参数
  • 原文地址:https://www.cnblogs.com/chy1000/p/5485717.html
Copyright © 2020-2023  润新知