温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

webdriver 自动化测试初试

发布时间:2020-08-06 19:39:06 来源:网络 阅读:770 作者:AnthonyGao1105 栏目:软件技术

之前已经搭建了测试需要的环境,也学习了locate elements的方法,下面我们就来创建第一个简单的自动化测试用例。

测试场景如下:

1.打开百度首页

2.在搜索框输入关键字搜索,比如:webdriver automation testing

3.点击百度一下button

4.验证搜索结果是否包含输入的关键字

用例自动化测试代码实例如下:

package com.example.tests;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.Assert;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;


public class BaiDuSearchTest {

private WebDriver driver;

private String baseUrl;


@BeforeMethod

        public void setUp() throws Exception {

    //Launch Firefox browser

   driver = new FirefoxDriver();

   baseUrl = "http://www.baidu.com";

 }


 @Test

        public void baiDuSearchTest() throws Exception {

    String exResult="WebDriver automation testing";

    //Open 百度  home page

   driver.get(baseUrl);

   //Locate search box and input search keyword

   driver.findElement(By.id("kw1")).sendKeys("WebDriver automation testing");

   //Click 百度一下 button

   driver.findElement(By.id("su1")).click();

   //在结果页面找到第一个link并验证搜索关键字显示在链接中

   String actResult=driver.findElement(By.id("1")).getText();

   Assert.assertTrue(actResult.contains(exResult));

   

 }


 @AfterMethod

public void tearDown() throws Exception {

   driver.quit();

 }


}


然后直接右击该java文件选择run as TestNG test,然后可以查看自动化测试用例的执行了。


最简单的一个测试用例就到这里了。是不是很easy?

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI