Selenium WebDriver上创建 WebDriver测试脚本
本文实现一个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种方式来执行脚本:
在eclipse的菜单栏点击执行按钮运行测试脚本,参见下图
在编辑器任意地方邮件点击,选择“Run As”选项,接下来选择“Java Application”
或者采用快捷键方式,按下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测试脚本的更多相关文章
- 实战二:LoadRunner创建一个测试脚本
问题一:执行脚本浏览器不能自动启动??? 原因:loadrunner11只支持IE9以下浏览器和火狐低版本浏览器 解决办法:1.IE浏览器取消勾选[启用第三方浏览器扩展]启动IE,从[工具]进入[In ...
- selenium框架安装及webdriver安装
本文介绍的是selenium安装及webdriver安装.小实例 1.selenium介绍 selenium是一个用于web应用程序测试的工具. Selenium测试直接运行在浏览器,就向真正的用户操 ...
- <译>Selenium Python Bindings 6 - WebDriver API
本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...
- Selenium (4) —— Selenium是什么? WebDriver是什么?做什么?(101 Tutorial)
Selenium (4) -- Selenium是什么? WebDriver是什么?做什么?(101 Tutorial) selenium版本: v2.48.0 (Standalone Seleniu ...
- 【Selenium】4.创建你的第一个Selenium IDE脚本
http://newtours.demoaut.com/ 这个网站将会用来作为我们测试的网址. 通过录制来创建一个脚本 让我们来用最普遍的方法——录制来创建一个脚本.然后,我们将会用回放的功能来执行录 ...
- selenium IDE & Remote Control & Webdriver
一直忘记写selenium的开始学习的过程,今天趁五一,天气有雨,写下这文章 1.进入selnium官网,了解selenium1,2,grid的区别.下载c#相关的包(使用c#的人非常少) 2.使用I ...
- Selenium执行测试脚本稳定性的一些经验分享交流
Selenium执行测试脚本稳定性的一些经验分享交流 公司的自动化WEB测试框架IATA已上线运行了一段时间,期间发现一些脚本稳定性的问题,与大家分享一下. CASE执行游览器:ie firefox ...
- 【关于selenium自动化中,Webdriver的原理以及工作流程】
原文地址:https://www.cnblogs.com/imyalost/p/7242747.html#4109245 作者:老 张 1.关于Webdriver 设计模式:按照Server-Clie ...
- py+selenium一个可被调用的登录测试脚本【待优化】
大部分系统现在都有登录页面,本文主要尝试写一个登录的测试脚本,及另一个脚本调用它登录测试已登录的页面模块. 目标: 登录脚本:从excel里获取登录的测试数据(包括异常测试)→执行登录脚本→输出是否通 ...
- [技术博客]基于动态继承类、WebDriver的浏览器兼容性测试框架搭建
问题背景 观察使用selenium进行自动化测试的过程,我们可以将它概述为: 启动测试进程,在该进程中构建WebDriver 启动浏览器进程,将它与WebDriver建立连接 使用WebDriver向 ...
随机推荐
- 安装Spring源码时报错No such property: values for class: org.gradle.api.internal.tasks.DefaultTas
IDEA进行项目拉取时:No such property: values for class: org.gradle.api.internal.tasks.DefaultTas 修改spring-be ...
- Qt编写地图综合应用10-点聚合
一.前言 点聚合在地图相关应用中比较常用,比如在地图上查询结果通常以标记点的形式展现,但是如果标记点较多,不仅会大大增加客户端的渲染时间,让客户端变得很卡,而且会让人产生密集恐惧症,密密麻麻的一大堆点 ...
- vue3 路由的使用
添加一个router.js 配置文件 import { createRouter, createWebHistory } from 'vue-router' createRouter:用来创建 路由 ...
- Sqlsugar 跨库查询小心得(同服务器不同数据库)
同一个服务器下的不同数据库,目前还没有进行跨服务器的查询,以后有待研究-- 1.使用的是Left Join左查询,因此连接字符串应该是写的第一个表所在的数据库的连接字符串 假设数据库A,B,连接字符串 ...
- 即时通讯技术文集(第19期):IM架构设计基础知识合集 [共13篇]
为了更好地分类阅读 52im.net 总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第19 期. [-1-] 微信后台基于时间序的新一代海量数据存储架构的设计实践 [链接] htt ...
- 16. C++快速入门--模板和Concept
待修改 1 定义模板 1.1 模板形参 模板参数 模板可以有两种参数, 一种是类型参数, 一种是非类型参数 这两种参数可以同时存在, 非类型参数 的类型 可以是 模板类型形参 template < ...
- C#/.NET/.NET Core技术前沿周刊 | 第 20 期(2025年1.1-1.5)
前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录.追踪C#/.NET/.NET Core领域.生态的每周最新.最实用.最有价值的技术文章.社区动态.优质项目和学习资源等. ...
- CDS标准视图:设备功能位置变更历史 I_EQUIPINSTALLATIONHISTORYC
视图名称:I_EQUIPINSTALLATIONHISTORYC 视图类型:基础视图 视图代码: 点击查看代码 @EndUserText.label: 'Equipment Installation ...
- C# 获取系统声卡音频数据,并绘制波形
//by wgscd //date:2022/11/7 UI: <Path Stroke="Red" Data="{Binding path}" Rend ...
- Redis中缓存预热、击穿、雪崩等问题解决方案-copy
1.缓存雪崩 缓存雪崩是指缓存同一时间大面积的失效,所以,后面的请求都会落到数据库上,造成数据库短时间内承受大量请求而崩掉. 解决方案 缓存数据的过期时间设置随机,防止同一时间大量数据过期现象发生. ...