zoukankan      html  css  js  c++  java
  • 【Selenium-WebDriver自学】Selenium-IDE不同的浏览器(八)

    ==========================================================================================================

        写在前面:

        Selenium IDE是FireFox的集成插件,目前而言,只能通过FireFox来录制脚本。

        但是实际工作中,我们可能需要测试IE或者Chrome等其他浏览器,那么,可以通过其他途径来实现这个愿望。详细看下文。

    ==========================================================================================================

    8. Selenium IDE- 不同的浏览器

    Selenium IDE脚本只能对火狐的工具Firefox插件运行测试。

    使用Selenium-IDE开发的测试可以对其他浏览器所保存为Selenium网络驱动器或硒的远程控制指令码执行。

    脚本只能对火狐的工具Firefox插件运行测试。

    使用Selenium-IDE开发的测试可以对其他浏览器所保存为Selenium网络驱动器或硒的远程控制指令码执行。

    更多关于Selenium的webdriver和Selenium的远程控制,在后面的章节有详细讲解。


    第1步:打开Selenium IDE任何已保存的测试

    第2步:定位到“File”菜单,并选择“Export Test Suite As”,而选择将被列出。

    第3步:现在让我们导出脚本“WebDriver”,并将其保存为同样的名称。

    第4步:如下图所示,显示保存webdriver文件。

    package com.example.tests;
    
    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.testng.annotations.*;
    import static org.testng.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    
    public class BaiduSearch1 {
      private WebDriver driver;
      private String baseUrl;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();
    
      @BeforeClass(alwaysRun = true)
      public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "https://www.baidu.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }
    
      @Test
      public void testBaiduSearch1() throws Exception {
        driver.get(baseUrl + "/");
        // ERROR: Caught exception [unknown command []]
        driver.findElement(By.id("kw")).clear();
        driver.findElement(By.id("kw")).sendKeys("selenium");
        driver.findElement(By.id("su")).click();
      }
    
      @AfterClass(alwaysRun = true)
      public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
          fail(verificationErrorString);
        }
      }
    
      private boolean isElementPresent(By by) {
        try {
          driver.findElement(by);
          return true;
        } catch (NoSuchElementException e) {
          return false;
        }
      }
    
      private boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }
    
      private String closeAlertAndGetItsText() {
        try {
          Alert alert = driver.switchTo().alert();
          String alertText = alert.getText();
          if (acceptNextAlert) {
            alert.accept();
          } else {
            alert.dismiss();
          }
          return alertText;
        } finally {
          acceptNextAlert = true;
        }
      }
    }

    以上为IDE导出的Java代码,可以通过其他途径来执行测试用例,运行到Chrome等浏览器上。

    ==========================================================================================================

        学习小结:

        1. 关于Selenium IDE的内容学习就告一段落了。

        Selenium IDE执行的局限性和录制脚本/回放脚本等,包括自己去面试的时候提到的录制脚本,被别人嘲笑,所以决定学习更多的关于Selenium
        方面的内容。

        那么接下来,将学习Selenium/WebDriver的实际开发,相信会是另外一番学习体验。

    ==========================================================================================================

  • 相关阅读:
    第四次实践作业
    第三次实践作业
    第二次实践作业
    第一次实践作业
    第02组 Beta版本演示
    第02组 Beta冲刺(4/4)
    大数据应用技术课程实践--选题与实践方案
    15 手写数字识别-小数据集
    14 深度学习-卷积
    13 垃圾邮件分类2
  • 原文地址:https://www.cnblogs.com/conquerorren/p/7239147.html
Copyright © 2011-2022 走看看