基于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实战例子的更多相关文章

  1. Appium移动自动化测试之Java篇

    1.环境准备:创建模拟器请参考:http://www.cnblogs.com/mrjade/p/5803131.html 2.新建一个java project,[File]-->[New]--& ...

  2. 自动化测试之旅--selenium+python--001

    在学习selenium之前,首先感谢网络上的虫师和乙醇老师,或许他们并不知道我这个菜鸟的存在,但是我仍然要感谢他们,因为在学习的路上拜读了许多他们的博客和文章,对于我来说有着很重要的意义,因此在学习之 ...

  3. Docker + Jenkins + Gitlab + Pytest + Allure 接口自动化测试之持续集成实战终极教程

    实战教程篇 前言 这边就不教大家怎么用 pytest 写项目了哦,下面有系列文章能帮助你快速入门 Pytest + Allure 这一篇教程主要是教如何从 0 到 1 搭建自动化测试的持续集成环境 后 ...

  4. java正则表达式实战例子,持续更新,记下来后面就不用重新写了。。。

    1.去掉HTML标签: /** * 去掉HTML外面的标签 * @author CY * */ public class TrimHTML { public static void main(Stri ...

  5. Selenium自动化测试之启动浏览器

    Selenium自动化测试之启动浏览器 一.Eclipse新建java工程 1.新建java工程:File->New->Java Project,输入Project name:如AutoT ...

  6. 玩玩自动化测试之selenium篇

    现如今社会科技发展太快了,纯功能点点点已经落后别人好几条街了,所以为了让自己多点职业生涯年限,得挺起肩,傲起头.自动化测试,其本质是用代码程序测试程序,所以其实第一步应该学好编程语言,后再自己开发自动 ...

  7. [转] Android自动化测试之使用java调用monkeyrunner(五)

    Android自动化测试之使用java调用monkeyrunner 众所周知,一般情况下我们使用android中的monkeyrunner进行自动化测试时,使用的是python语言来写测试脚本.不过, ...

  8. selenium自动化测试之整合测试报告

    selenium自动化测试之整合测试报告 标签(空格分隔): 整合报告 如下截图我们添加一个文件叫做:latest_report.py文件, import time import os import ...

  9. Selenium自动化测试之结果处理

    Selenium自动化测试之结果处理 一.断言 断言相当于性能测试中的检查点,常用断言种类很多,具体可以查看断言API:判断预期结果和实际结果是否一致,断言成功,程序继续处理,失败则终止运行,示例如下 ...

随机推荐

  1. [持续交付实践] Jenkins Pipeline 高可用设计方法

    前言 这篇写好一段时间了,一直也没发布上来,今天稍微整理下了交下作业,部分内容偷懒引用了一些别人的内容.使用Jenkins做持续集成/持续交付,当业务达到一定规模的时候,Jenkins本身就很容易成为 ...

  2. Oracle参数Arraysize设置对于逻辑读的影响分析

    说明: 当执行一条SQL查询的时候,为了获得满足的数据,查询在这个过程中完成解析,绑定,执行和提取数据等一系列步骤,这些步骤都是单独执行的,满足条件的数据行必须由数据库返回给应用:对于任何大小的结果集 ...

  3. kubernetes nginx ingress controller部署

    Kubernetes nginx ingress controller部署 1.下载kubernetes nginx的yaml文件 Wget https://raw.githubusercontent ...

  4. oracle增加记录谁在连接你的数据库

    我们都知道在v$session 中记录着客户端的机器名称,但是没有IP , 如果记录clinet ip 呢? 有两种思路: ①    利用trigger,后面就是这种方式 ②    利用 DBMS_S ...

  5. Crontab 执行时没有环境变量!

    Crontab 执行时没有环境变量! Crontab 执行时没有环境变量! Crontab 执行时没有环境变量! 重要的事情说三遍,浪费我半天时间去找问题!! 非系统默认工具,执行时候需要加全路径!!

  6. Python第10天

    装饰器:本质上是函数,为其他函数添加附件功能. 装饰器 = 高阶函数 + 函数嵌套 + 闭包 原则(开放封闭原则):1,不修改被修饰函数代码.2,不修改被修饰函数调用方式. @方法名

  7. docx httpheader头设置

    设置contentType内容类型如下: Extension MIME Type .doc application/msword .dot application/msword .docx appli ...

  8. Shell 批量修改主机 用户密码

    问题:132.121.114 和 132.121.118 网段共 48 台主机未添加基础监控,但是 wh 账户不能登录 需进行批量修改密码操作. 目前情况:op1对上述48台机器设备均能免密登录. 操 ...

  9. thinkphp5.1 判断是不是post提交

    if(Request::isPost()){ }else{ } 这样就对了

  10. 熟悉Junit单元测试方法

    定义: JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己 ...