本文实现一个WebDriver测试脚本,介绍WebDrive的常用命令、UI元素定位的策略以及在脚本中的使用,还有Get命令。

你将学到:

 脚本创建

 代码走查

 测试执行

 定位Web元素

 定位符类型及其语法

 总结

一. 脚本创建

脚本创建部分仍然使用之前创建的“Learning Selenium”项目和“gmail.com”作为被测试应用程序(AUT)。

场景:

启动浏览器,打开“Gmail.com”。

验证页面标题并打印验证结果。

输入用户名和密码。

单击登录按钮。

关闭web浏览器。

步骤1:在“Learning Selenium”项目下创建一个名为“Gmail_Login”的新java类名称

步骤2:在“Gmail_Login”中复制并粘贴以下代码

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Gmail_Login {

/**

  • @param args

    */

    public static void main(String[] args) {

// objects and variables instantiation

WebDriver driver = new FirefoxDriver();

String appUrl = "https://accounts.google.com";

// launch the firefox browser and open the application url

driver.get(appUrl);

// maximize the browser window

driver.manage().window().maximize();

// declare and initialize the variable to store the expected title of the webpage.

String expectedTitle = " Sign in - Google Accounts ";

// fetch the title of the web page and save it into a string variable

String actualTitle = driver.getTitle();

// compare the expected title of the page with the actual title of the page and print the result

if (expectedTitle.equals(actualTitle))

{

System.out.println("Verification Successful - The correct title is displayed on the web page.");

}

else

{

System.out.println("Verification Failed - An incorrect title is displayed on the web page.");

}

// enter a valid username in the email textbox

WebElement username = driver.findElement(By.id("Email"));

username.clear();

username.sendKeys("TestSelenium");

// enter a valid password in the password textbox

WebElement password = driver.findElement(By.id("Passwd"));

password.clear();

password.sendKeys("password123");

// click on the Sign in button

WebElement SignInButton = driver.findElement(By.id("signIn"));

SignInButton.click();

// close the web browser

driver.close();

System.out.println("Test script executed successfully.");

// terminate the program

System.exit(0);

}

}

上面的代码的意思与前面的文本场景相同

二. 代码走查

导入语句:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.By;

在编码前,我们需要导入上面的包:

import org.openqa.selenium.WebDriver:引用WebDriver的接口,该接口用于实例化一个新的Web 浏览器
import org.openqa.selenium.firefox.FirefoxDriver:引用FirefoxDriver类,该类在WebDriver的接口上实例化Firefox特定驱动程序
import org.openqa.selenium.WebElement:引用WebElement类,该类用于实例化一个新的web元素。
import org.openqa.selenium.By:引用By 类,用于调用定位符
有时我们需要引入其他几个包来实现更复杂、更独特的功能,如excel操作、数据库连接、日志记录、断言等

对象实例化

WebDriver driver = new FirefoxDriver();

创建一个引用变量WebDriver,并使用FirefoxDriver类实例化它。这个过程将启动一个默认的Firefox配置文件,而不加载任何扩展和插件。

启动Web浏览器

driver.get(appUrl);

在WebDriver实例上调用get()方法来启动新的web浏览器实例。Get()方法的字符串将web浏览器重定向应用程序的URL

浏览器窗口最大化

driver.manage().window().maximize();

maximize()方法是在浏览器窗口被重定向到应用程序URL后,将其最大化。

获取页面标题

driver.getTitle ();

获取当前网页的标题,可以将获取的标题加载到字符串变量中。

if (expectedTitle.equals(actualTitle))

{

System.out.println("Verification Successful - The correct title is displayed on the web page.");

}

else

{

System.out.println("Verification Failed - An incorrect title is displayed on the web page.");

}

上面代码的意思是用java结构将期望的结果和实际的结果进行比较,根据比较的结果进行输出。

WebElement实例化

WebElement username = driver.findElement(By.id(“Email”));

在上面的语句中,我们通过调用“driver.findElement(By.id(“Email”))”,实例化了WebElement的引用。同时,用户名可以通过引用用户界面上的电子邮件文本框,实现对用户界面的一些操作

清除命令

username. Clear();

clear()方法/命令用于清除文本框中出现的值,包括清除默认值

sendKeys命令

username.sendKeys(“TestSelenium “);

sendKeys()方法/命令用于在文本框中输入/键入指定的值,上面的代码意思是在Gmail应用程序的电子邮件文本框中输入字符串“TestSelenium”, sendKeys是webdriver脚本中最常用的命令之一。

Click命令

SignInButton.click();

与sendKeys()类似,click()是另一个与web元素交互的命令。

单击()命令/方法用于单击web页面上的web元素。

上面的代码意思是在Gmail应用程序上单击“Sign in”按钮

注:

与sendKeys()方法不同,click()方法不能参数化。

为了支持单击web元素可能会加载一个新页面这种情况,click()方法是等待页面加载的编码方式。

关闭Web浏览器

driver.close();

close()用于关闭当前浏览器窗口。

终止Java程序

System.exit(0);

Exit()方法强制终止Java程序。记住在终止Java程序之前关闭所有浏览器实例。

三. 测试执行

可以有下面3种方式来执行脚本:

  1. 在eclipse的菜单栏点击执行按钮运行测试脚本,参见下图

  2. 在编辑器任意地方邮件点击,选择“Run As”选项,接下来选择“Java Application”

  3. 或者采用快捷键方式,按下Ctril+F11组合键

执行成功后,在面板上显示“Test script executed successfully”

四. 定位Web元素

WebDriver中的Web元素定位和检查可以像在Selenium IDE的前面文章中介绍的那样,使用Selenium IDE和Firebug可以检查GUI上的web元素。强烈建议使用Selenium IDE来查找web元素。找到web元素后,复制并粘贴目标值到WebDriver代码中。

在WebDriver中,web元素是在动态查找器(findElement)的帮助下定位的(findElement(By.locatorType(“locator value”))).

比如:

driver.findElement(By.id(“Email”));

定位符类型及其语法

五. 总结

在本文中,我们使用WebDriver和Java开发了一个自动化脚本。我们还讨论了构成WebDriver脚本的各种组件。

重点内容

 在编写脚本前,我们需要导入一些能够创建WebDriver脚本的包

importopenqa.selenium.By;

importopenqa.selenium.WebDriver;

importopenqa.selenium.WebElement;

importopenqa.selenium.firefox.FirefoxDriver;

 get()方法打开新的浏览器,get()方法的字符串将启动web浏览器并重定向到应用程序的URL

 maximize()方法使窗口最大化

 clear() 方法可清楚文本框里的任何内容

 sendKeys() 方法在文本框中输入指定的值

 Click()方法用于在web页面上点击web元素

 在WebDriver中,可以使用动态查找器定位web元素

 可用的定位器类型:

id

className

name

xpath

cssSelector

linkText

partialLinkText

tagName

Selenium WebDriver上创建 WebDriver测试脚本的更多相关文章

  1. 实战二:LoadRunner创建一个测试脚本

    问题一:执行脚本浏览器不能自动启动??? 原因:loadrunner11只支持IE9以下浏览器和火狐低版本浏览器 解决办法:1.IE浏览器取消勾选[启用第三方浏览器扩展]启动IE,从[工具]进入[In ...

  2. selenium框架安装及webdriver安装

    本文介绍的是selenium安装及webdriver安装.小实例 1.selenium介绍 selenium是一个用于web应用程序测试的工具. Selenium测试直接运行在浏览器,就向真正的用户操 ...

  3. <译>Selenium Python Bindings 6 - WebDriver API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  4. Selenium (4) —— Selenium是什么? WebDriver是什么?做什么?(101 Tutorial)

    Selenium (4) -- Selenium是什么? WebDriver是什么?做什么?(101 Tutorial) selenium版本: v2.48.0 (Standalone Seleniu ...

  5. 【Selenium】4.创建你的第一个Selenium IDE脚本

    http://newtours.demoaut.com/ 这个网站将会用来作为我们测试的网址. 通过录制来创建一个脚本 让我们来用最普遍的方法——录制来创建一个脚本.然后,我们将会用回放的功能来执行录 ...

  6. selenium IDE & Remote Control & Webdriver

    一直忘记写selenium的开始学习的过程,今天趁五一,天气有雨,写下这文章 1.进入selnium官网,了解selenium1,2,grid的区别.下载c#相关的包(使用c#的人非常少) 2.使用I ...

  7. Selenium执行测试脚本稳定性的一些经验分享交流

    Selenium执行测试脚本稳定性的一些经验分享交流 公司的自动化WEB测试框架IATA已上线运行了一段时间,期间发现一些脚本稳定性的问题,与大家分享一下. CASE执行游览器:ie firefox ...

  8. 【关于selenium自动化中,Webdriver的原理以及工作流程】

    原文地址:https://www.cnblogs.com/imyalost/p/7242747.html#4109245 作者:老 张 1.关于Webdriver 设计模式:按照Server-Clie ...

  9. py+selenium一个可被调用的登录测试脚本【待优化】

    大部分系统现在都有登录页面,本文主要尝试写一个登录的测试脚本,及另一个脚本调用它登录测试已登录的页面模块. 目标: 登录脚本:从excel里获取登录的测试数据(包括异常测试)→执行登录脚本→输出是否通 ...

  10. [技术博客]基于动态继承类、WebDriver的浏览器兼容性测试框架搭建

    问题背景 观察使用selenium进行自动化测试的过程,我们可以将它概述为: 启动测试进程,在该进程中构建WebDriver 启动浏览器进程,将它与WebDriver建立连接 使用WebDriver向 ...

随机推荐

  1. 解决httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0

    httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for Se ...

  2. Qt编写安防视频监控系统23-图片地图

    一.前言 图片地图这个模块是后面新增加进去的,主要是安防领域还有很多应用场景是一个区域比如就一个学校,提供一个学校的平面图或者鸟瞰图,然后在该地图上放置对应的摄像机,双击该摄像机图标可以查看对应的实时 ...

  3. vue3 路由的使用

    添加一个router.js 配置文件 import { createRouter, createWebHistory } from 'vue-router' createRouter:用来创建 路由 ...

  4. 开源即时通讯IM框架MobileIMSDK的微信小程序端技术概览

    一.基本介绍 MobileIMSDK - 微信小程序端是一套基于微信原生 WebSocket 的即时通讯库: 1)超轻量级.无任何第 3 方库依赖(开箱即用): 2)纯 JS 编写.ES6 语法.高度 ...

  5. 【Java 温故而知新系列】基础知识-03 基本类型对应之包装类

    1.包装类都有哪些? 基本类型都有对应的包装类型,这些包装类提供了一种面向对象的方式来处理基本数据类型,允许它们被用于需要对象的场景,如集合框架.泛型等. 对应关系: 基本类型 包装类型 boolea ...

  6. API接口请求小结

    API接口请求小结 一.python: API接口请求 1.1 multipart/form-data类型请求 参数类型:数组 1.2 multipart/form-data类型请求 参数类型:文件流 ...

  7. weixueyuan-Nginx核心配置指令2

    https://www.weixueyuan.net/nginx/config/ Nginx配置文件详解 Nginx 默认编译安装后,配置文件都会保存在 /usr/local/nginx/conf 目 ...

  8. Vanity Intermediate 统配符提权

    nmap扫描 ┌──(root㉿kali)-[~] └─# nmap -p- -A 192.168.167.234 Starting Nmap 7.94SVN ( https://nmap.org ) ...

  9. 0511-FileWrite字符输出流和JDK7中try..finally新的特性

    package A10_IOStream; import java.io.*; /* java.io.Writer:字符输出流,是所有字符数出流的最顶层抽象父类 共性方法 void write(int ...

  10. dart中类详细讲解

    dart是一门面向对象的语言 dart是一门实用类和单继承的面向对象的语言 在dart中所有的对象都是类的实例. 所有的类都是Object的子类 类都是有属性和方法组成的 定义一个类 在dart中,我 ...