Electorn(桌面应用)自动化测试之Java+selenium实战例子
基于electorn的桌面应用,网上相关资料较少。所有记录一下。使用java+selenium+testng对该类型应用的自动化测试方法。
代码样例
package com.contract.web.cases;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ElectronTest {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");// You can skip this if chromedriver is already included in the PATH.
ChromeOptions options = new ChromeOptions();
options.setBinary("F:\\软件包\\B2\\B2测试\\B2.exe");//设置二进制文件,一定用绝对路径,不要用/的写法
DesiredCapabilities capabilities = new DesiredCapabilities();//负责启动服务端时的参数设置
capabilities.setCapability(ChromeOptions.CAPABILITY, options);//将参数options添加到设置中
ChromeDriver driver = new ChromeDriver(capabilities);//欺骗chromdriver启动electron
// Now, your electron app would have been opened.
// Now if you open the dev tools using CMD+ALT+I you would notice two dev tools and first one being for the electron shell. We need to switch to the second window handle. Let's do that.
Thread.sleep(5000);
for (String handle : driver.getWindowHandles())
{
driver.switchTo().window(handle); // Since there are two window handles this would switch to last one(which is second one). You can also explicitly provide the window number.
}
// If you inspect using the Dev Tools, you would notice the second window Dev Tools corresponds to actual page you have opened.
// From here you can write the usual selenium script and it will work.
driver.findElement(By.xpath("//a[@ng-click='login()']")).click();
Thread.sleep(5000);
//跳转后,会生成新的窗口,所以要跳转到最后这一个窗口,才能找到元素
for (String handle : driver.getWindowHandles())
{
driver.switchTo().window(handle); // Since there are two window handles this would switch to last one(which is second one). You can also explicitly provide the window number.
}
Thread.sleep(3000);
driver.findElement(By.xpath("//span[text()='进货']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[text()='销售']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[text()='库存']")).click();
}
}
思路:
创建驱动,打开electorn。
获取句柄操作元素
testng运用。就先获取句柄再进行操作。如下先封装一个基类,然后编写的测试方法调用即可:
基类:
package com.contract.web.cases;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import com.contract.web.pojo.UIElement;
import com.contract.web.util.UILibraryUtil;
public class BaseElectron {
private Logger logger = Logger.getLogger(BaseElectron.class);
public static WebDriver driver;
@BeforeSuite
@Parameters(value={"electronType","driverPath"})
public void init(String electronType,String driverPath) throws Exception{
logger.info("配置信息:ELectron版本:【"+electronType+"】,驱动文件路径:【"+driverPath+"】");
System.setProperty("webdriver.chrome.driver",driverPath);// You can skip this if chromedriver is already included in the PATH.
ChromeOptions options = new ChromeOptions();
options.setBinary(electronType);//设置二进制文件,一定用绝对路径,不要用/的写法
DesiredCapabilities capabilities = new DesiredCapabilities();//负责启动服务端时的参数设置
capabilities.setCapability(ChromeOptions.CAPABILITY, options);//将参数options添加到设置中
logger.info("*************创建了chrome驱动对象,打开了Electron,开始测试*****************");
driver = new ChromeDriver(capabilities);//欺骗chromdriver启动electron
// Now, your electron app would have been opened.
// Now if you open the dev tools using CMD+ALT+I you would notice two dev tools and first one being for the electron shell. We need to switch to the second window handle. Let's do that.
Thread.sleep(5000);
for (String handle : driver.getWindowHandles())
{
System.out.println(handle);
driver.switchTo().window(handle); // Since there are two window handles this would switch to last one(which is second one). You can also explicitly provide the window number.
}
// If you inspect using the Dev Tools, you would notice the second window Dev Tools corresponds to actual page you have opened.
// From here you can write the usual selenium script and it will work.
driver.findElement(By.xpath("//a[@ng-click='login()']")).click();
Thread.sleep(5000);
//跳转后,会生成新的窗口,所以要跳转到最后这一个窗口,才能找到元素
for (String handle : driver.getWindowHandles())
{
System.out.println(handle);
driver.switchTo().window(handle); // Since there are two window handles this would switch to last one(which is second one). You can also explicitly provide the window number.
}
}
@AfterSuite
public void tearDown() throws InterruptedException{
Thread.sleep(3000);
logger.info("************测试完成,关闭驱动对象***********");
driver.quit();
}
}
测试用例例子:
package com.contract.web.cases2;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import com.contract.web.cases.BaseElectron;
public class Purcharse extends BaseElectron {
@Test(priority=0)
public void successcase(){
for (String handle : driver.getWindowHandles())
{
System.out.println(handle);
driver.switchTo().window(handle); // Since there are two window handles this would switch to last one(which is second one). You can also explicitly provide the window number.
}
//driver.findElement(By.xpath("//span[text()='分析']")).click();
click(getElement("进货页", "进货"));
}
}
Electorn(桌面应用)自动化测试之Java+selenium实战例子的更多相关文章
- Appium移动自动化测试之Java篇
1.环境准备:创建模拟器请参考:http://www.cnblogs.com/mrjade/p/5803131.html 2.新建一个java project,[File]-->[New]--& ...
- 自动化测试之旅--selenium+python--001
在学习selenium之前,首先感谢网络上的虫师和乙醇老师,或许他们并不知道我这个菜鸟的存在,但是我仍然要感谢他们,因为在学习的路上拜读了许多他们的博客和文章,对于我来说有着很重要的意义,因此在学习之 ...
- Docker + Jenkins + Gitlab + Pytest + Allure 接口自动化测试之持续集成实战终极教程
实战教程篇 前言 这边就不教大家怎么用 pytest 写项目了哦,下面有系列文章能帮助你快速入门 Pytest + Allure 这一篇教程主要是教如何从 0 到 1 搭建自动化测试的持续集成环境 后 ...
- java正则表达式实战例子,持续更新,记下来后面就不用重新写了。。。
1.去掉HTML标签: /** * 去掉HTML外面的标签 * @author CY * */ public class TrimHTML { public static void main(Stri ...
- Selenium自动化测试之启动浏览器
Selenium自动化测试之启动浏览器 一.Eclipse新建java工程 1.新建java工程:File->New->Java Project,输入Project name:如AutoT ...
- 玩玩自动化测试之selenium篇
现如今社会科技发展太快了,纯功能点点点已经落后别人好几条街了,所以为了让自己多点职业生涯年限,得挺起肩,傲起头.自动化测试,其本质是用代码程序测试程序,所以其实第一步应该学好编程语言,后再自己开发自动 ...
- [转] Android自动化测试之使用java调用monkeyrunner(五)
Android自动化测试之使用java调用monkeyrunner 众所周知,一般情况下我们使用android中的monkeyrunner进行自动化测试时,使用的是python语言来写测试脚本.不过, ...
- selenium自动化测试之整合测试报告
selenium自动化测试之整合测试报告 标签(空格分隔): 整合报告 如下截图我们添加一个文件叫做:latest_report.py文件, import time import os import ...
- Selenium自动化测试之结果处理
Selenium自动化测试之结果处理 一.断言 断言相当于性能测试中的检查点,常用断言种类很多,具体可以查看断言API:判断预期结果和实际结果是否一致,断言成功,程序继续处理,失败则终止运行,示例如下 ...
随机推荐
- 如何用Fiddler手机抓包
截获智能手机发出的HTTP包有什么用? 用处一: 手机软件程序员利用Fiddler,可以截获手机发出的HTTP包, 从而调试程序: 用处二: 软件测试人员用于测试智能手机上的软件: 用处三: 可以用来 ...
- 【C语言基础】什么是字节?
字节就是存储数据的单位,并且是硬件所能访问的最小单位. 一个字节控制8位 int 类型为4个字节 long 类型为8个字节 char 类型为1个字节:Java中为两个字节
- springboot学习随笔(一):springboot环境构建:eclipse+maven+jdk1.8
一:所需环境 1.jdk1.8(配置环境变量,可自行搜索相关文档) 2.maven(maven的配置不在赘述,可自行搜索相关文档) 3.eclipse(第三种方式,eclipse集成sts时需要,直接 ...
- jsonp现实跨域Ajax CORS
浏览器有一个很重要的概念——同源策略(Same-Origin Policy).所谓同源是指,域名,协议,端口相同.不同源的客户端脚本(javascript.ActionScript)在没明确授权的情况 ...
- windows cmd.exe 将程序 stdout 输出到文件中
问题背景:通过 cmd.exe 调用程序,会有一些输出信息,在 cmd 中不方便查阅,所以需要导入文件中. 例如 方法: 可以在其路径下看到
- U3D 设置帧率与垂直同步
1,设置帧率: Application.targetFrameRate = 60: //-1为无限制 2,垂直同步 project settings -> quality,任何level的垂直同 ...
- eval方法
1.作用 eval函数可计算某个字符串,并执行其中的Javascript代码 2.参数 eval函数的参数为一个string类型的字符串,不能是String()类型的对象 3.返回值 计算string ...
- Alpha冲刺
第一天 日期:2018/6/16 1.今日完成任务情况以及遇到的问题 张天旭:根据系统的需求,完成数据库的设计 周甜甜:完成系统后台登录界面的设计及登录功能的实现 李蕾:完成系统后台首页的设计 张海鑫 ...
- CSS 背景图像 background属性简写
background属性简写 background属性可以像margin padding属性一样,有简写方法,它的简写顺序是: background-color background-image ba ...
- vuecli3.0安装搭建项目
1. npm install -g @vue/cli 2. vue create wechat Linter / Formatter 可以不选 检查空格的 //选择less //标准eslint // ...