2016-11-17 46 views
0

是否有示例java代碼用於使用硒 - chromedriver啓動電子應用程序?這是我在這裏的地方。我的代碼帶來了電子應用程序,我可以在該頁面上查看webElements,但不會啓動我需要測試的應用程序。我可以將應用程序拖到電子窗口並啓動,但WebDriver並未指向它。測試在MacOS環境下使用硒,java的電子應用程序

private void electronTest() throws Exception { 

    //select electron-chromedriver 
    System.setProperty("webdriver.chrome.driver", "/Users/username/work/node_modules/electron-chromedriver/bin/chromedriver"); 

    ChromeOptions options = new ChromeOptions(); 
    // path for Electron 
    options.setBinary("/Users/username/work/app/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron"); 
    // I have tried both the folder and the app 
    options.addArguments("/Users/username/work/app/out/packages/mac/electronApplication.app"); 

    DesiredCapabilities capabilities = new DesiredCapabilities(); 
    capabilities.setCapability("chromeOptions", options); 
    capabilities.setBrowserName("chrome"); 

    driver = new ChromeDriver(capabilities); 
    // have also tried... 
    //driver = new RemoteWebDriver(new URL("http://localhost:9515"), capabilities); 

    // Electron page appears, but doesn't launch the Electron app 

    // driver is pointing to the electron page elements even if I drag the app to launch it 
    String screenText = " [" + driver.findElement(By.tagName("BODY")).getText().replace("\n", "][") + "]"; 
    System.out.println("screenText " + screenText); 
} 
+0

製作相反發射電子,並試圖路徑傳遞到構建的進步......,我已經建立的應用程序,我設置的二進制MacOS的建立。應用程序中:options.setBinary ( 「/Users/username/work/ape/out/packages/mac/appName.app/Contents/MacOS/appName」);這現在調出應用程序本身,但有必要抓住第二個窗口句柄並在出現窗口時執行driver.switchTo。 – AJW

回答

0

我可以給出一個在macOS上完美工作的例子,因爲我最近一直在測試它們。

public void electronTest() 
{ 
    System.setProperty("webdriver.chrome.driver","path to the chromedriver");// You can skip this if chromedriver is already included in the PATH. 

    ChromeOptions options = new ChromeOptions(); 
    options.setBinary("/Applications/YourApp.app/Contents/MacOS/YourApp"); 
    DesiredCapabilities capabilities = new DesiredCapabilities(); 
    capabilities.setCapability(ChromeOptions.CAPABILITY, options); 
    driver = new ChromeDriver(capabilities); 


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

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

    // Let's navigate to a page 
    driver.navigate().to(URL); 
    // 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. 
} 
相關問題