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("進貨頁", "進貨"));}}

相關文章
相關標籤/搜索