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:判断预期结果和实际结果是否一致,断言成功,程序继续处理,失败则终止运行,示例如下 ...
随机推荐
- nagios nrpe
- 使用jieba库与wordcloud库第三方库进行词频统计
一.jieba库与wordcloud库的使用 1.jieba库与wordcloud库的介绍 jieba 库的分词原理是利用一个中文词库,将待分词的内容与分词词库进行比对,通过图结构和动态规划方法找到最 ...
- ActiveMQ(3)---ActiveMQ原理分析之消息持久化
持久化消息和非持久化消息的存储原理 正常情况下,非持久化消息是存储在内存中的,持久化消息是存储在文件中的.能够存储的最大消息数据在${ActiveMQ_HOME}/conf/activemq.xml文 ...
- AS3获得当前文件的文件名
//当前文件的完整路径var fileUrl:String = this.loaderInfo.url;//查找路径的最后一个"/"var flag:int = fileUrl.l ...
- 49.字符串转int
面360的时候这道题目加了一个要求就是要求小数输出整数,故增加一个关于小数点的判断 要注意转义字符:"."和"|"都是转义字符,必须得加"\\&quo ...
- HASHSET不能预留容量问题
如果,它没有像 var lst = new List<int>(32) 这样可以直接预留容量的操作,原因未知. 但可以通过如下操作进行预留: //给hashset预留容量,防止动态扩容,由 ...
- c# linq 汇总
例子:List<string> list = new List<string>(); list.Add("1 a");list.Add("2 b& ...
- linux大文件读取
在生产环境中有时候可能会遇到大文件的读取问题,但是大文件读取如果按照一般的手法.如cat这种都是对io的一个挑战,如果io扛得住还好,如果扛不住 造成的后果,如服务器内存奔溃,日志损坏 方法一: se ...
- 配置错误 不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 overrideMode="Deny" 或旧有的 allowOverride="false" 的位置标记明确设置的。
原因:可能是在安装IIS7的时候没有安装asp.net, 尝试使用以下方法: cmd.exe要以管理员身份启动,在c:\windows\system32下找到cmd.exe,右键管理员启动,输入命令 ...
- 一.javascript核心部分:1.词法结构
本文作为个人学习笔记,一直也没有重视javascript的系统学习(javascript是最容易被人忽视的语言),我都是要用的时候百度一下查找下资料开始用,但没有系统的,学习,和整理过javascri ...