Skip to content

[🚀 Feature]: Does Selenium support automated testing of electron applications based on Windows platform? #16106

@3255834892

Description

@3255834892

Description

This is my current approach. The problem with this approach is that every time I need to jump to a page, I need to manually switch to the corresponding xapp page, and it doesn't seem to support selenium grid. Is there any good way to deal with it?

package Test;
mport com.Test.AbstractComponents.MatPeekDeepZip;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Properties;

public class BaseTest2 {

public WebDriver driver;
Properties prop = new Properties();


public static void killPortIfBusy(int port) throws IOException, InterruptedException {

 
    Process find = new ProcessBuilder(
            "cmd", "/c",
            "netstat -ano | findstr :" + port + " | findstr LISTENING")
            .redirectErrorStream(true)
            .start();

    String line;
    try (BufferedReader br = new BufferedReader(
            new InputStreamReader(find.getInputStream()))) {
        line = br.readLine();                
    }

    if (line == null || line.isBlank()) {
        System.out.println( port);
        return;
    }

    // 样例行:TCP    127.0.0.1:9222   0.0.0.0:0   LISTENING   1456
    String pid = line.trim()               
            .replaceAll(" +", " ")  
            .split(" ")[4];        

    System.out.println( port + pid + " Kill");

    
    Process kill = new ProcessBuilder(
            "taskkill", "/F", "/PID", pid)
            .inheritIO()                      
            .start();

    int exit = kill.waitFor();
    if (exit != 0) {
        throw new IOException("taskkill 失败,退出码 " + exit);
    }

  
    Thread.sleep(500);
  
}

/* 启动服务端口 */
public WebDriver initializeDriver() throws Exception {
    FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+
            "//src//main//resources//GlobalData.properties");
    prop.load(fis);
    killPortIfBusy(Integer.parseInt(prop.getProperty("StartPort")));
    new ProcessBuilder(prop.getProperty("AppPath"),
            "--remote-debugging-port=" + prop.getProperty("StartPort"),
            "--no-sandbox",
            "--")
            .redirectErrorStream(true)
            .redirectOutput(ProcessBuilder.Redirect.INHERIT)
            .start();
    return driver;
}

/* Chrome连接服务 */
public WebDriver ConnectChrome() throws MalformedURLException {
    ChromeOptions opts = new ChromeOptions();
    opts.setExperimentalOption("debuggerAddress", "127.0.0.1:" + prop.getProperty("StartPort"));
    ChromeDriverService svc = new ChromeDriverService.Builder()
            .usingDriverExecutable(new File(prop.getProperty("DriverPath")))
            .withBuildCheckDisabled(true)
            .build();
    driver = new ChromeDriver(svc, opts);
    return driver;
}



@Test
public void testLogin() {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='normal_login_username']"))).sendKeys("admin");
    driver.findElement(By.xpath("//*[@id='normal_login_password']")).sendKeys("admin");
}



@BeforeMethod(alwaysRun = true)
public void landingPage() throws Exception {
    driver=initializeDriver();
    driver=ConnectChrome();

}

@AfterMethod
public void tearDown() throws Exception {
    driver.close();
    driver.quit();
  
}

}

Have you considered any alternatives or workarounds?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-needs-triagingA Selenium member will evaluate this soon!C-javaJava BindingsI-enhancementSomething could be betterI-questionApplied to questions. Issues should be closed and send the user to community resources.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions