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向 ...
随机推荐
- maven打jar包运行main方法
pom文件添加上这段 <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</ ...
- Macos 安装md5sum、sha1sum、md5deep、sha1deep
一.安装md5sum和sha1sum 方法一:brew 安装 # brew install md5sha1sum 方法二:编译安装 源码下载地址:http://www.microbrew.org/to ...
- 波折重重:Linux实时系统Xenomai宕机问题的深度定位
目录 一 前言 二 背景 三 原因分析及措施 硬件原因 应用软件 操作系统 四 分析定位 转机 拨云见雾 irq计数 Schedstat coreclk 现象结论 五 原因一 六 原因二 七 解决 八 ...
- Qt数据库应用9-数据导出组件使用方法
一.使用方法 1.1 第一步:引入组件 组件中所有代码文件是一个整体,不支持单个代码文件拆分使用,因为很多通用的方法都放在一个代码文件中,复用很多代码. datehead是本组件用到的头文件以及通用的 ...
- [转]Clion+mingw环境下Assimp编译
1.Clion+mingw环境下Assimp编译 2.MinGW-w64下载
- 开源轻量级IM框架MobileIMSDK的鸿蒙NEXT客户端库已发布
一.基本介绍 MobileIMSDK-鸿蒙端是一套基于鸿蒙Next(纯血鸿蒙)系统的IM即时通讯客户端库: 1)超轻量级(编译后库文件仅50KB).无任何第3方库依赖(开箱即用): 2)纯ArkTS编 ...
- FreeSWITCH日志功能分析及apr模拟
操作系统版本:Debian 12.5_x64 FreeSWITCH版本: 1.10.11 apr库版本:apr-1.7.4 & apr-util-1.6.3 gcc版本: 12.2.0 日 ...
- .NET Core GC对象 分配(GC Alloc)底层原理浅谈
对象分配策略 .NET程序的对象是由CLR控制并分配在托管堆中,如果是你,会如何设计一个内存分配策略呢? 按需分配,要多少分配多少,移动alloc_ptr指针即可,没有任何浪费.缺点是每次都要向OS申 ...
- 利用SDCC开源项目搭建C51编译平台
下载sdcc 安装sdcc 安装sublime 新建编译系统输入以下内容 { "shell_cmd": "sdcc \"${file}\" " ...
- G1原理—4.G1垃圾回收的过程之Young GC
大纲 1.G1的YGC过程 2.YGC并行处理阶段的过程 3.YGC串行处理阶段的过程(一) 4.YGC串行处理阶段的过程(二) 5.整个YGC的执行流程总结 1.G1的YGC过程 (1)YGC相关的 ...