Electorn(桌面应用)自动化测试之Java+selenium实战例子

基于electorn的桌面应用,网上相关资料较少。全部记录一下。使用java+selenium+testng对该类型应用的自动化测试方法。java

代码样例web

package com.contract.web.cases;chrome

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;shell

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.apache

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();
}app

}electron

思路:ide

建立驱动,打开electorn。测试

获取句柄操做元素ui

 

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("进货页", "进货"));}}

相关文章
相关标签/搜索