mercredi 3 octobre 2018

Java: Only One Driver (ChromeDriver) of Seven is Working (Usage of One Thread for each Driver)

I am using one Thread for one WebDriver (ChromeDriver) which works (it opens multiple ChromeDrivers) but unfortnuately, it only uses one WebDriver (probably only the focused one?).

My Goal is, that each WebDriver is supposed to do some stuff, independent from all other WebDrivers. So each Webdriver executes exactly one method.

I want to use MultiThreading because of the Performance (if I'm doing everything sequential, my program takes 50 seconds, so i want to reduce that with multithreading which would raise the performance of my program!)

So here is Multi-Threading-Method:

public void getAll(ScrapingISA scrapingIsaObject) {
    Method[] methods = this.getClass().getDeclaredMethods();
    for(Method method: methods) {
        if(method.getName().startsWith("get") && method.getName().length() > 6 
                && !method.getName().equals("getKurzzeichen")) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        method.invoke(scrapingIsaObject);
                    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}

The method which is invoked makes a login. The Login Method instantiate a new WebDriver. So Each Method and so also each Thread, makes first a login and the login Method instantiate a new WebDriver -> Each thread uses an own Webdriver.

Here my login-Method to fully understand:

private boolean login() throws LoginFailedException {
    openDriver();
    if(navigate())
        driver.navigate().to(url);
    else 
        throw new LoginFailedException();
    try {
        driver.findElement(By.id("field_username")).sendKeys(kurzzeichen);
        WebElement passwortElement = driver.findElement(By.name("ww_x_password"));
        if(containsSpecialCharForSelenium(passwort)) 
            ((JavascriptExecutor)driver).executeScript("arguments[0].value = arguments[1]", passwortElement, passwort);
        else
            passwortElement.sendKeys(passwort);
        driver.findElement(By.xpath("//input[@value='Login']")).click();
        if(!checkIfLoginSucceeded()) {
            logger.log(Level.INFO, "Login für den Benutzer ''{0}'' fehlgeschlagen - bitte prüfe deine Logindaten", kurzzeichen);
            return false;
        }
        return true;
    } catch (NoSuchElementException|IllegalArgumentException ex) {
        logger.log(Level.INFO, "Login fehlgeschlagen: Web-Elemente für das Login nicht gefunden\n" + ex.getMessage());
        closeDriver();
        return false;           
    } 
}

Here my openDriver Method:

public void openDriver() {
    this.driver = new ChromeDriver();
    this.driver.manage().window().maximize();;
    this.driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
}

So What happens: It opens 7 Browser (which is exactly what I want) and then in only ONE Browser it makes the input, on the other 6 browser it does not make anything. So the login does not work at all! Just for Information, here also the navigate Method:

public boolean navigate() {
    int statusCode;
    try {
        HttpURLConnection http = (HttpURLConnection) new URL(url).openConnection();
        statusCode = http.getResponseCode();
    } catch (IOException ex) {
        logger.log(Level.INFO, "Verbindung zum Server fehlgeschlagen oder URL ''{0}'' nicht korrekt", url);
        closeDriver();
        return false;
    }
    if(statusCode == 200)
        return true;
    else {
        logger.log(Level.INFO, "Unerlaubter Zugriff auf die URL ''{0}'' (HTTP 401 - Unauthorized)", url);
        closeDriver();
        return false;
    }
}

I'd appreciate any Help or Advise!





Aucun commentaire:

Enregistrer un commentaire