• Selenium入门教程


    1)Selenium 简介

    什么是Selenium?
    1. Selenium最初是由ThoughtWorks公司一个叫Jason R. Huggins的工程师和他的团队开发出来
    2. Selenium是仅针对Web系统的一款自动化测试工具
    3. Selenium是免费的、开源的,很多公司选择Selenium和它是免费的有很大关系 Selenium不是一个工具,它是一系列工具的总称
    4. Selenium支持多种编程语言 Selenium支持多种浏览器 Selenium可以运行在多个平台上
    Selenium 3.0之后可以说主要包括两个工具:
    • Selenium IDE(火狐57之后的版本中不再支持)
    • Selenium RC(3.0之后基本也已经从Selenium核心的安装包中去除掉了)
    • Selenium Webdriver(Selenium 2.0 以后的核心,用于控制浏览器)
    • Selenium Grid(支持脚本在多台机器上并行执行脚本,提升执行效率)
    使用Selenium做自动化测试是需要掌握一门语言的,Selenium支持很多编程语言:

    Java C# PHP Python Perl Ruby

    Selenium支持很多浏览器:
    • Internet Explorer
    • Microsoft Edge
    • Firefox
    • Google Chrome
    • Opera
    • HtmlUnit
    Selenium也可以运行在很多操作系统上:

    Windows/Mac/Linux

    2)Selenium 环境搭建

    • 配置JDK环境
    • 下载Selenium关联的jar/使用maven配置 参考
    • 下载浏览器驱动
    • 远程执行的场合,下载并在远程机器上安装Selenium Server (Grid)

    3)Selenium 使用

    WebDriver 如何驱动浏览器的

    Selenium-WebDriver 直接通过浏览器自动化的本地接口来调用浏览器。
    如何直接调用,和调用的细节取决于你使用什么浏览器。本章后续的内容介绍了每个 “browser driver” 的详细信息。

    WebDriver 和 Selenium-Server

    你可能需要,也可能不需要 Selenium Server,取决于你打算如何使用 Selenium-WebDriver。
    如果你仅仅需要使用 WebDriver API,那就不需要 Selenium-Server。
    如果你所有的测试和浏览器都在一台机器上,那么你仅需要 WebDriver API。WebDriver 将直接操作浏览器。
    在有些情况下,你需要使用 Selenium-Server 来配合 Selenium-WebDriver 工作,例如:
    1)你使用 Selenium-Grid 来分发你的测试给多个机器或者虚拟机。
    2)你希望连接一台远程的机器来测试一个特定的浏览器。
    3)你没有使用 Java 绑定(例如 Python, C#, 或 Ruby),并且可能希望使用 HtmlUnit Driver。

    WebDriver分类

    1)RemoteWebDriver
    2)浏览器对应driver

    • InternetExplorerDriver
    • EdgeDriver
    • FirefoxDriver
    • ChromeDriver
    WebDriver使用

    1)设置driver的环境变量,加载WebDriver实例
    2)获取一个页面
    driver.get(“http://www.google.com”);

    3)查找 UI 元素(web 元素)
    Byid

    WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
    
    • 1

    ByClassName

    List<WebElement> cheeses = driver.findElements(By.className("cheese"));
    
    • 1

    ByTagName

    WebElement frame = driver.findElement(By.tagName("iframe"));
    
    • 1

    ByName

    WebElement cheese = driver.findElement(By.name("cheese"));
    
    • 1

    ByLinkText

    WebElement cheese = driver.findElement(By.linkText("cheese"));
    
    • 1

    ByPartialLinkText

    WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
    
    • 1

    ByCSS

    WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy"));
    
    • 1

    ByXPATH

    WebElement cheese = driver.findElement(By.xpath(xpath));
    
    • 1

    4)元素等待

    inputBox = fluentWait(By.xpath(xpath));
    
    • 1

    5)选择(select)/取消选择
    Select类

    select_by_index(index)/deselect_by_index(index)
    select_by_value(value)/deselect_by_value(value)
    select_by_visible_text(text)/deselect_by_visible_text(text)
    
    • 1
    • 2
    • 3

    6)用户输入
    SendKeys可以给element做输出,也可以作为动作快捷键,比如ctrl+c,ctrl +v ,alt,enter等

    inputBox.sendKeys(text);
    
    • 1

    7)用户输入 - 填充表单
    如果你已经完成表单填充,你可能希望提交它,你只要找到 “submit” 按钮然后点击它即可。

    driver.findElement(By.id("submit")).click();
    
    • 1

    或者,你可以调用 WebDriver 为每个元素提供的 “submit” 方法。如果你对一个 form 元素调用该方法,WebDriver 将调用这个 form 的 submit 方法。如果这个元素不是一个 form,将抛出一个异常。

    element.submit();
    
    • 1

    8)在窗口和帧(frames)之间切换

    driver.switchTo().window("windowName");
    driver.switchTo().frame("frameName");
    
    • 1
    • 2

    9)Cookies
    在我们继续介绍更多内容之前,还有必要介绍一下如何操作 cookie。首先,你必须在 cookie 所在的域。如果你希望在加载一个大页面之前重设 cookie,你可以先访问站点中一个较小的页面,典型的是 404 页面
    // 进到正确的域

    driver.get("http://www.example.com");
    
    • 1

    // 设置 cookie,这个cookie 对整个域都有效

    Cookie cookie = new Cookie("key", "value");
    driver.manage().addCookie(cookie);
    
    • 1
    • 2

    // 输出当前 url 所有可用的 cookie

    Set<Cookie> allCookies = driver.manage().getCookies();
    for (Cookie loadedCookie : allCookies) {
        System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
    }
    
    // 你可以通过3中方式删除 cookie
    // By name
    driver.manage().deleteCookieNamed("CookieName");
    // By Cookie
    driver.manage().deleteCookie(loadedCookie);
    // Or all of them
    driver.manage().deleteAllCookies();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    10)拖拽
    以下代码演示了如何使用 “Actions” 类来实现拖拽。浏览器本地方法必须要启用:

    WebElement element = driver.findElement(By.name("source"));
    WebElement target = driver.findElement(By.name("target"));
    
    (new Actions(driver)).dragAndDrop(element, target).perform();
    
    • 1
    • 2
    • 3
    • 4

    4)Selenium API官方文档

    https://www.selenium.dev/selenium/docs/api/java/overview-summary.html
    https://wizardforcel.gitbooks.io/selenium-doc/content/index.html

  • 相关阅读:
    工业4.0时代数字化工厂的几个特点
    npm版本升级报错
    探索Facebook的创新实验室:社交媒体的研发之旅
    CSS总结——瀑布流布局
    什么是Linux
    学习编程语言需要熟悉库函数吗?
    STM32F407ZGT6移植AD7606
    【矩阵论】2. 矩阵分解——单阵及特征值特征向量一些求法
    解题-->在线OJ(十三)
    计算机毕业设计ssm+vue基本微信小程序的健康管理系统
  • 原文地址:https://blog.csdn.net/qq_38933606/article/details/127899713