Web自动化测试 Selenium 1/3

 🔥 交流讨论:欢迎加入我们一起学习!

🔥 资源分享耗时200+小时精选的「软件测试」资料包

🔥 教程推荐:火遍全网的《软件测试》教程  

📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!

Selenium 名字的来源

在这里,我还想说一下关于 Selenium 名字的来源,很有意思的 : > : Selenium 的中文名为 “ 硒 ” ,是一种化学元素的名字,它 对 汞 ( Mercury )有天然的解毒作用,实验表明汞暴露水平越高,硒对汞毒性的拮抗作用越明显,所以说硒是汞的克星。大家应该知道 Mercury 测试工具系 列吧( QTP , QC , LR , WR... ),他们功能强大,但却价格不菲,大家对此又爱又恨!故 thoughtworks 特意把他们的 Web 开源测试工具命 名为 Selenium ,以此帮助大家脱离汞毒。

产品类别

Selenium IDE

一个用于构造测试脚本的原型工具。它是一个Firefox插件,并且提供了一个易于使用的开发自动化测试的接口。Selenium IDE有一个录制功能,可以记录用户执行的动作,然后可以导出它们作可重用的脚本

Remote Control

Selenium RC是最重要的Seleniumx项目,在WebDriver/Selenium合并产生Selenium 2

WebDriver

Selenium 2是该项目的未来方向,和对Selenium工具包的最新的增加物。

Grid

如果你必须运行你的测试集在多个环境,你可以有不同的远程机器的支持和运行你的测试在同一时间在不同的远程机器上。在任何一种情形下,Selenium都将充分利用并行处理,极大地改善运行你的测试所花费的时间。

浏览器支持

官方文档 http://docs.seleniumhq.org/docs/01_introducing_selenium.jsp#supported-browsers-and-platforms

实战操作

准备

IE Chrome的Driver安装和准备

https://code.google.com/p/selenium/wiki/ChromeDriver

https://code.google.com/p/selenium/wiki/InternetExplorerDriver

RemoteControl的不同浏览器Java代码

package base;

import org.openqa.selenium.*;

import org.openqa.selenium.ie.*;

import org.openqa.selenium.remote.*;

import static org.testng.Assert.*;

import org.testng.annotations.*;

import com.thoughtworks.selenium.Selenium;

import java.io.*;

import java.net.*;

public class BaseRC {

protected Selenium selenium;

private WebDriver driver = null;

private StringBuffer verificationErrors = new StringBuffer();

@Parameters({ "platform", "browser", "version", "url" })

@BeforeTest(alwaysRun = true)

public void setup(String platform, String browser, String version,

String url) throws MalformedURLException, IOException {

DesiredCapabilities caps = null;

// Browsers

if (browser.equalsIgnoreCase("Internet Explorer")) {

System.setProperty("webdriver.ie.driver",

"c:\\test\\IEDriverServer.exe");

caps = DesiredCapabilities.internetExplorer();

// IE安全设置

caps.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,

true);

// browser zoom level must be set to 100%

} else if (browser.equalsIgnoreCase("Firefox")) {

System.setProperty("webdriver.firefox.bin",

"C:\\test\\Firefox4\\firefox.exe");

caps = DesiredCapabilities.firefox();

} else if (browser.equalsIgnoreCase("chrome")) {

System.setProperty("webdriver.chrome.driver",

"c:\\test\\chromedriver.exe");

caps = DesiredCapabilities.chrome();

caps.setCapability(

"chrome.binary",

"C:\\test\\Chrome31\\chrome.exe");

} else if (browser.equalsIgnoreCase("iPad"))

caps = DesiredCapabilities.ipad();

else if (browser.equalsIgnoreCase("Android"))

caps = DesiredCapabilities.android();

// Platforms

if (platform.equalsIgnoreCase("Windows"))

caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);

else if (platform.equalsIgnoreCase("MAC"))

caps.setPlatform(org.openqa.selenium.Platform.MAC);

else if (platform.equalsIgnoreCase("Andorid"))

caps.setPlatform(org.openqa.selenium.Platform.ANDROID);

// Version

caps.setVersion(version);

driver = new RemoteWebDriver(new URL(

"http://localhost:4444/wd/hub"), caps);

selenium = new WebDriverBackedSelenium(driver, url);

}

@AfterTest

public void afterTest() {

// Close the browser

driver.quit();

selenium.stop();

String verificationErrorString = verificationErrors.toString();

if (!"".equals(verificationErrorString)) {

fail(verificationErrorString);

}

}

}

WebDriver的不同浏览器Java代码

package base;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.*;

import org.openqa.selenium.ie.*;

import org.openqa.selenium.chrome.*;

import org.openqa.selenium.remote.*;

import org.openqa.selenium.support.ui.ExpectedCondition;

import org.openqa.selenium.support.ui.WebDriverWait;

import static org.testng.Assert.*;

import org.testng.annotations.*;

import java.io.*;

import java.net.*;

public class BaseWebDriver {

protected WebDriver driver = null;

private StringBuffer verificationErrors = new StringBuffer();

@Parameters({ "platform", "browser", "version", "url" })

@BeforeTest(alwaysRun = true)

public void setup(String platform, String browser, String version,

String url) throws MalformedURLException, IOException {

DesiredCapabilities caps = null;

// Browsers

if (browser.equalsIgnoreCase("Internet Explorer")) {

System.setProperty("webdriver.ie.driver",

"c:\\test\\IEDriverServer.exe");

caps = DesiredCapabilities.internetExplorer();

// IE安全设置

caps.setCapability(

InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,

true);

// browser zoom level must be set to 100%

} else if (browser.equalsIgnoreCase("Firefox")) {

System.setProperty("webdriver.firefox.bin",

"C:\\test\\Firefox4\\firefox.exe");

caps = DesiredCapabilities.firefox();

} else if (browser.equalsIgnoreCase("chrome")) {

System.setProperty("webdriver.chrome.driver",

"c:\\test\\chromedriver.exe");

caps = DesiredCapabilities.chrome();

caps.setCapability(

"chrome.binary",

"C:\\test\\Chrome31\\chrome.exe");

} else if (browser.equalsIgnoreCase("iPad"))

caps = DesiredCapabilities.ipad();

else if (browser.equalsIgnoreCase("Android"))

caps = DesiredCapabilities.android();

// Platforms

if (platform.equalsIgnoreCase("Windows"))

caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);

else if (platform.equalsIgnoreCase("MAC"))

caps.setPlatform(org.openqa.selenium.Platform.MAC);

else if (platform.equalsIgnoreCase("Andorid"))

caps.setPlatform(org.openqa.selenium.Platform.ANDROID);

// Version

caps.setVersion(version);

driver = new RemoteWebDriver(new URL(

"http://localhost:4444/wd/hub"), caps);

driver.get(url);

//

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(new ExpectedCondition<WebElement>() {

@Override

public WebElement apply(WebDriver d) {

return d.findElement(By.id("login"));

}

});

}

/* @Test(description = "TestDemo")

public void testDemo() throws InterruptedException {

// Sleep until the div we want is visible or 5 seconds is over

long end = System.currentTimeMillis() + 5000;

while (System.currentTimeMillis() < end) {

WebElement resultsDiv = driver.findElement(By.id("container"));

// If results have been returned, the results are displayed in a

// drop down.

if (resultsDiv.isDisplayed()) {

break;

}

}

}*/

@AfterTest

public void afterTest() {

// Close the browser

driver.quit();

//driver.close();

String verificationErrorString = verificationErrors.toString();

if (!"".equals(verificationErrorString)) {

fail(verificationErrorString);

}

}

}

Grid下的不同浏览器运行脚本

总控运行

rem  http://localhost:4444/grid/console 可以查看hub总控的信息

java -jar selenium-server-standalone-2.35.0.jar -role hub -port 4444 -nodeTimeout 600

各种浏览器运行的脚本

参数设置相同的部分[IP RC/WD运行模式]

@echo off

set a=0

for %%a in (%*) do set /a a+=1

echo "%a% argc"

Rem 可变的设置

set PORT=8902 

if %a%==1 (

if "%1%"=="" (

 set IP="localhost"

) else (

 set IP=%1%

)

set MODE="webdriver"

) else (

if "%1%"=="" (

 set IP="localhost"

) else (

 set IP=%1%

)

if "%2%"=="rc" (

 set MODE="node"

 set PORT=9902

) else (

 set MODE="webdriver"

)

)

echo %IP% %MODE%

不同浏览器的运行参数

java -Dwebdriver.chrome.driver="c:\test\chromedriver.exe" -jar selenium-server-standalone-2.35.0.jar -role %MODE% -hubHost %IP% -port %PORT% -timeout 20000 -browser "browserName=chrome,version=31,maxInstances=2,platform=WINDOWS,chrome.binary=C:\test\Chrome31\chrome.exe"

rem java -jar selenium-server-standalone-2.35.0.jar -h 可以查看帮助参数

rem !!! -browser参数中,逗号之间不要有空格

java -jar selenium-server-standalone-2.35.0.jar -role %MODE% -hubHost %IP% -port %PORT% -timeout 20000 -browser "browserName=firefox,version=4,maxInstances=1,platform=WINDOWS,firefox_binary=C:\test\Firefox4\firefox.exe"

java -Dwebdriver.ie.driver="c:\test\IEDriverServer.exe" -jar selenium-server-standalone-2.35.0.jar -role %MODE% -hubHost %IP% -port %PORT% -timeout 20000 -browser "browserName=internet explorer,version=8,maxInstances=1,platform=WINDOWS"

最后我邀请你进入我们的【软件测试学习交流群:785128166】, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路

作为一个软件测试的过来人,我想尽自己最大的努力,帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源,现在免费分享给大家,有需要的小伙伴可以关注【公众号:程序员二黑】自提!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/400570.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Spring Framework

Spring Framework Spring 是一款开源的轻量级 Java 开发框架&#xff0c;旨在提高开发人员的开发效率以及系统的可维护性。 Spring 框架指的都是 Spring Framework&#xff0c;它是很多模块的集合&#xff0c;如下图所示&#xff1a; 一、Core Container Spring 框架的核心模…

Atcoder ABC341 B - Foreign Exchange

Foreign Exchange&#xff08;外汇&#xff09; 时间限制&#xff1a;2s 内存限制&#xff1a;1024MB 【原题地址】 所有图片源自Atcoder&#xff0c;题目译文源自脚本Atcoder Better! 点击此处跳转至原题 【问题描述】 【输入格式】 【输出格式】 【样例1】 【样例输入1…

【JavaEE】_form表单构造HTTP请求

目录 1. form表单的格式 1.1 form表单的常用属性 1.2 form表单的常用搭配标签&#xff1a;input 2. form表单构造GET请求实例 3. form表单构造POST请求实例 4. form表单构造法的缺陷 对于客户端浏览器&#xff0c;以下操作即构造了HTTP请求&#xff1a; 1. 直接在浏览器…

AcWing 2868. 子串分值(贡献法)

AcWing 2868. 子串分值 原题链接&#xff1a;https://www.acwing.com/problem/content/2871/ 具体分析过程如下图&#xff1a; 直接遍历的话太麻烦&#xff0c;且时间复杂度太高&#xff0c;所以另寻他路 字符串中只有小写字母26个&#xff0c;所以可以从此着手&#xff0c; …

Netty中的PooledByteBuf池化原理剖析

PooledByteBuf PooledByteBuf是池化的ByteBuf&#xff0c;提高了内存分配与释放的速度&#xff0c;它本身是一个抽象泛型类&#xff0c; 有三个子类:PooledDirectByteBuf、PooledHeapByteBuf、PooledUnsafeDirectByteBuf. Jemalloc算法 Netty的PooledByteBuf采用与jemalloc一…

java在当前项目创建文件

public static void main(String[] arge) throws IOException {File file new File("dade02\\dade");//不存在创建if(!file.exists()){file.mkdirs();}File file1 new File("dade02\\dade\\daade.txt");file1.createNewFile();}或者 public static void …

Rust ?运算符 Rust读写txt文件

一、Rust &#xff1f;运算符 &#xff1f;运算符&#xff1a;传播错误的一种快捷方式。 如果Result是Ok&#xff1a;Ok中的值就是表达式的结果&#xff0c;然后继续执行程序。 如果Result是Err&#xff1a;Err就是整个函数的返回值&#xff0c;就像使用了return &#xff…

租用海外服务器,自己部署ChatGPT-Next-Web,实现ChatGPT聊天自由,还可以分享给朋友用

前言 如果有好几个人需要使用ChatGPT&#xff0c;又没有魔法上网环境&#xff0c;最好就是自己搭建一个海外的服务器环境&#xff0c;然后很多人就可以同时直接用了。 大概是情况是要花80元租一个一年的海外服务器&#xff0c;花15元租一个一年的域名&#xff0c;然后openai 的…

搜索专项---DFS之连通性模型

文章目录 迷宫红与黑 一、迷宫OJ链接 本题思路:DFS直接搜即可。 #include <iostream> #include <cstring> #include <algorithm>constexpr int N110;int n; char g[N][N]; bool st[N][N]; int x1, y1, x2, y2;int dx[4] {-1, 0, 1, 0}, dy[4] {0, 1, 0, …

多线程完成文件拷贝:2024/2/21

作业1&#xff1a;使用多线程完成两个文件的拷贝 要求&#xff1a;第一个线程拷贝前一半&#xff0c;第二个线程拷贝后一半&#xff0c;主线程回收两个线程的资源 代码&#xff1a; #include <myhead.h>//定义结构体 typedef struct Info {char *src;char *dest;int s…

springboot访问webapp下的jsp页面

一&#xff0c;项目结构。 这是我的项目结构&#xff0c;jsp页面放在WEB-INF下的page目录下面。 二&#xff0c;file--->Project Structure,确保这两个地方都是正确的&#xff0c;确保Source Roots下面有webapp这个目录&#xff08;正常来说&#xff0c;应该本来就有&#…

c语言经典测试题1

1.题1 int x5,y7; void swap() { int z; zx; xy; yz; } int main() { int x3,y8; swap(); printf("%d,%d\n"&#xff0c;x, y); return 0; } A: 5,7 B: 7,5 C: 3,8 D: 8,3 大家思考一下选哪一个呢&#xff1f; 我们来分析一下&#xff1a;上述代码中我们创建了4…

【JavaWeb】网上蛋糕商城-项目搭建

学习目标 了解网上蛋糕商城的项目需求 了解网上蛋糕商城的功能结构 熟悉E-R图和数据表的设计 熟悉项目环境的搭建 通过前面章节的学习&#xff0c;相信读者应该已经掌握了Web开发的基础知识&#xff0c;学习这些基础知识就是为开发Web网站奠定基础。如今&#xff0c;电子商…

golang JSON数据格式 XML数据格式 Gob(这玩意真的有人用吗?)

接着摸鱼&#xff0c;摸鱼一时爽&#xff0c;一直摸鱼一直爽&#xff0c;接着搞golang 不是所有的数据都可以编码为 JSON 类型&#xff0c;只有验证通过的数据结构才能被编码&#xff1a; JSON 对象只支持字符串类型的 key&#xff1b;要编码一个 Go map 类型&#xff0c;map 必…

stm32——hal库学习笔记(DAC)

这里写目录标题 一、DAC简介&#xff08;了解&#xff09;1.1&#xff0c;什么是DAC&#xff1f;1.2&#xff0c;DAC的特性参数1.3&#xff0c;STM32各系列DAC的主要特性 二、DAC工作原理&#xff08;掌握&#xff09;2.1&#xff0c;DAC框图简介&#xff08;F1&#xff09;2.2…

Hive JDBC

Hive远程模式搭建好之后&#xff0c;可以使用Beeline客户端或JDBC远程访问Hive了 启动HiveServer2服务 $ hive --service hiveserver2 & 新建Java Maven项目&#xff0c;在pom.xml中添加以下依赖 <dependencies><dependency><groupId>jdk.tools</g…

曝光一下不发年终奖的企业

原文连接&#xff1a; 曝光一下不发年终奖的企业 今日热帖&#xff0c;看到网上发布的一篇帖子&#xff1a;请曝光一下不发年终奖的企业&#xff01; 结果留言上百条&#xff0c;除了私企&#xff0c;还有很多国企&#xff0c;银行等。而且还有一些我们认为应该很赚钱的企业&a…

opencv图像放缩与插值-resize函数

在OpenCV中&#xff0c;resize函数用于对图像进行尺寸调整&#xff08;放大或缩小&#xff09;&#xff0c;这个过程中通常需要用到插值方法来计算新尺寸下图像像素的值。插值方法对于放缩的质量有着直接影响。 void resize(InputArray src, OutputArray dst, Size dsize, dou…

Linux 性能分析工具汇总

Linux 性能分析工具汇总 出于对Linux操作系统的兴趣&#xff0c;以及对底层知识的强烈欲望&#xff0c;因此整理了这篇文章。本文也可以作为检验基础知识的指标&#xff0c;另外文章涵盖了一个系统的方方面面。如果没有完善的计算机系统知识&#xff0c;网络知识和操作系统知识…

k-邻近算法(kNN)

目录 k-近邻算法概述 k-近邻算法的一般流程 kNN算法伪代码 k-近邻算法概述 优点&#xff1a;精度高、对异常值不敏感、无数据输入假定 缺点&#xff1a;计算复杂度高、空间复杂度高 适用数据范围&#xff1a;数值型和标称型 k-近邻算法的一般流程 &#xff08;1&#x…
最新文章