java与自动化的应用实例_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("进货页", "进货"));

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值