• 自动化测试的根本:通过元素定位,准确操作测试对象


    一、基本元素操作方法

    (1)获取网页title,输出结果:getTitle();

    (2)获取URL,输出结果:getCurrentUrl()

    (3)获取文本信息,输出结果:getText();

    (4)获取输入框长度,输出结果:getSize();

    (5)判断是否被选中,输出结果:isSelected();

    (6)判断是否可见,输出结果:isDisplayed();

    (7)判断是否可编辑,输出结果:isEnabled();

    (8)文本框键入,输出结果:sendKeys("sel");

    (9)点击按钮操作:click();

    (10)清空文本框内容:clear();

    (11)关闭浏览器操作:close();


    二、实例测试内容为百度页面,代码如下:

    package TestCase;
    import java.util.List;
    import org.junit.Assert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.testng.annotations.Test;
    public class NewTest {
    @Test
      public void test1() throws InterruptedException {
      // 指定IEDriverServer安装位置
      System.setProperty("webdriver.ie.driver", "D:\PYJ-Soft\Selenium\IEDriverServer.exe");
      // 启动 IE 浏览器
      WebDriver driver =new InternetExplorerDriver();
      // 打开百度网址
      driver.get("http://www.baidu.com");
      // (1)获取 getTitle(),并输出结果
      System.out.println(driver.getTitle()); 
      // (2)获取当前页面的URL,并输出结果
      System.out.println(driver.getCurrentUrl());
      // (3)获取页面菜单栏文本信息,并输出结果
      List <WebElement> element=driver.findElements(By.xpath(".//*[@id='u1']/a"));
      for (int i =0;i<element.size();i++) {
        String text  = element.get(i).getText();
        System.out.println(text); 
      }
      // (4)获取输入框长度信息,并输出结果
      System.out.println(driver.findElement(By.id("kw")).getSize());
      // (5)判断是否被选中,并输出结果
      boolean xz =  driver.findElement(By.xpath("//*[@id='u1']/a[1]")).isSelected();
      System.out.println("校验是否选中结果:"+ xz);
      // (6)判断百度一下按钮是否可见,并输出结果
      boolean su = driver.findElement(By.id("su")).isDisplayed();
      Assert.assertTrue("校验百度一下按钮是否显示", su);
      System.out.println(su);
      // (7)判断输入框是否处于可编辑状态,并输出结果
      boolean kw = driver.findElement(By.id("kw")).isEnabled();
      Assert.assertTrue("校验输入框是否为可编辑状态", kw);
      System.out.println(kw);
      // (8)定位输入框,键入内容操作(selenium)
      driver.findElement(By.id("kw")).sendKeys("selenium");  
      // (9)定位按钮操作, 点击按钮进行检索内容,并展示
      driver.findElement(By.id("su")).click();
      // 进入等待时间
      Thread.sleep(3000);
      //(10)清空键入内容(selenium)
      driver.findElement(By.id("kw")).clear();
      // 进入等待时间
      Thread.sleep(3000);
      //(11) 操作完毕,关闭程序
      driver.close();           
      }
    }

    三、代码运行结果:

    [RemoteTestNG] detected TestNG version 7.0.0

    Started InternetExplorerDriver server (64-bit)

    2.53.0.0

    Listening on port 39138

    Only local connections are allowed

    百度一下,你就知道

    https://www.baidu.com/

    新闻

    hao123

    地图

    视频

    贴吧

    学术

    登录

    设置

    更多产品

    (500, 22)

    校验是否选中结果:false

    true

    true

    PASSED: test1

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

        Default test

        Tests run: 1, Failures: 0, Skips: 0

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

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

    Default suite

    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

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

  • 相关阅读:
    遥控器拆卸记录
    计算器拆卸记录
    no matching constructor for initialization
    STL
    排序方法
    二叉树之广度优先遍历
    C++之queue学习记录
    方向电路
    站间联系电路
    求二叉树的最大深度
  • 原文地址:https://www.cnblogs.com/-pyj/p/12107781.html
Copyright © 2020-2023  润新知