selenium webdriver 实践演练

自动化演练步骤及代码:

将自动执行以下测试操作:

  • 调用Firefox浏览器。
  • 打开网址: www.baidu.com
  • 点击百度搜索文本框。
  • 输入关键字 - “易百教程”
  • 单击“搜索”按钮。

接下来将逐步创建测试用例,以便详细了解每个组件。

1步 - 启动Eclipse IDE并打开在本教程的上一节(配置Selenium WebDriver)中创建的项目“Demo_Test” 。在“Demo_Test” 测试套件下的“FirstTestCase.java” 文件中编写第一个Selenium测试脚本。

注意:要在Selenium中调用浏览器,必须下载特定于该浏览器的可执行文件。 例如,Chrome浏览器使用名为ChromeDriver.exe 的可执行文件实现WebDriver协议。 这些可执行文件在您的系统上启动服务器,而该服务器又负责在Selenium中运行测试脚本。

2步 - 在浏览器中打开网址:

3步 - 点击对应操作系统版本的“geckodriver”链接,并安您所使用使用的当前操作系统下载,在编写此文章时,所使用的时Win10 64位操作系统,所以下载:
geckodriver-v0.23.0-win64.zip 。下载的文件将采用压缩格式,将内容解压缩到一个方便的目录中。

4步 - 需要为百度搜索文本框和搜索按钮等网络元素添加唯一标识,以便通过测试脚本自动执行这些标识。 这些唯一标识与一些命令/语法一起配置以形成定位器。 使定位器在Web应用程序的上下文中定位和标识特定的Web元素。

用于查找唯一标识元素的方法涉及检查HTML代码。

在Chrome浏览器中打开网址 : https://www.baidu.com 。右键单击百度搜索文本框,然后选择Inspect Element 。
https://img-blog.csdnimg.cn/20181217195032977

它将启动一个窗口,其中包含测试盒开发中涉及的所有特定代码。选择id元素的值,即“kw”
https://img-blog.csdnimg.cn/20181217195032994

下面给出了在Selenium WebDriver中通过“id” 定位元素的Java语法。

driver.findElement(By.id (<element ID>));

Java

以下是在测试脚本中查找百度搜索文本框的完整代码。

driver.findElement(By.id ("kw"));

Java

现在,右键单击百度搜索按钮(百度一下)并选择Inspect Element ,如下图所示:
https://img-blog.csdnimg.cn/2018121719503311

它将启动一个窗口,其中包含开发百度搜索按钮所涉及的所有特定代码 ,如下图所示:
https://img-blog.csdnimg.cn/2018121719503329

选择id元素的值,即“su” ,如下图所示:
https://img-blog.csdnimg.cn/2018121719503353

下面给出了在Selenium WebDriver中通过“name”定位元素的Java语法。

driver.findElement(By.name (<element name>));

Java

以下是在测试脚本中查找百度搜索按钮的完整代码。

// driver.findElement(By.name ("btnK")); 这里用不上。

driver.findElement(By.id ("su"));

Java

5步 - 接下来编写代码,为每个代码块写上注释,以便清楚地解释这些步骤。

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;

public class FirstTestCase {
    public static void main(String[] args) {
        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.gecko.driver", "D:\\software\\WebDriver\\geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
        WebDriver driver = (WebDriver) new FirefoxDriver();
        // Launch website
        // driver.navigate().to("http://www.baidu.com/");
        driver.get("http://www.baidu.com/");
        //driver.manage().window().maximize();
        String titile = driver.getTitle();
        System.out.println("title is => " + titile);
         // Click on the search text box and send value          	driver.findElement(By.id("kw")).sendKeys("易百教程");  
        // Click on the search button  
        driver.findElement(By.id("su")).click();  
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //driver.quit();
    }
}

一些可能遇到的错误:

未设置驱动路径时

java.lang.IllegalStateException报错:

System.setProperty("webdriver.gecko.driver", "D:\\software\\geckodriver.exe");

//若无法打开Firefox浏览器,可设定Firefox浏览器的安装路径(未设置路径时path报错)

System.setProperty("webdriver.firefox.bin", "D:\\software\\firefox、、firefox.exe");

Eclipse代码窗口如下所示:

6步 - 右键单击Eclipse代码,然后选择:Run As -> Java Application 。

7步 - 上述测试脚本将启动Firefox浏览器,并执行搜索。如下图所示 -
https://img-blog.csdnimg.cn/2018121719503372

 

 

代码解释:

导入包/语句
在java中,import语句用于导入另一个包中存在的类。 简单来说,import关键字用于将内置和用户定义的包导入java源文件。

org.openqa.selenium.WebDriver - 引用实例化新Web浏览器所需的WebDriver接口。

org.openqa.selenium.chrome.ChromeDriver - 引用将Chrome专用驱动程序实例化到WebDriver类实例化的浏览器所需的ChromeDriver类。

实例化对象和变量
通过以下方式实例化驱动程序对象:

WebDriver driver=new ChromeDriver();

Java


启动网站
要启动新网站,在WebDriver中使用navigate().to()方法。

driver.navigate().to("http://www.yiibai.com/");

// 或者

driver.get("http://www.baidu.com/");

单击元素
在WebDriver中,用户交互是通过使用Locators来执行的,在本教程的后续会话中讨论。 目前,以下代码实例用于定位和解析特定Web元素中的值。

driver.findElement(By.id("su")).sendKeys("易百教程");


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值