1

Firstable, I would like to apologize for my english, I'll try my best to be as clear as possible.

I encounter a problem with Selenium, in Java. Indeed, I use a portal, and I need to be logged in to be able to browse on it. Each time I open this portal, I have to connect, a pop-up window appears. I have to use a specific account to do a specific action, and then I have to disconnect from this account and reconnect with another account to manage some things. I am able to close the pop-up which appears when I click on the “disconnect” button with a robot, with the “Enter” key. And then, I am blocked. The window disappears, ok, but the program crashes and tells me this error: “Modal dialog present: Demande de confirmation - Confirmez-vous la fermeture du portail ?” After this step, I can’t even open the portal again. I have also a connection matter, in fact the browser reminds my user and password, but I need to change account. So I thought about erasing cookies. I am a bit lost.

My code is :

        driver.findElement(By.id("disconnectBtn")).click();
        WaitUtils.waitFor(5000);
        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_ENTER);


        WaitUtils.waitFor(5000);


        driver.manage().deleteAllCookies();
        System.out.println("I have deleted cookies.");


        allerSurPortail("L1", driver);
        System.out.println("I have opened a second time the portal.");
        accueilPortailPage.fermerFrame(driver);
        WaitUtils.waitFor(5000);

My function "fermerFrame" permits closing a pop-up which appears when I am connected on the website, it works.

To resume:

  • My program opens the portal which shows a pop-up to connect.
  • Once connected, I have to do some things on the website (things I don't do for the test)
  • I click on the disconnect button, this pop-up appears Disconnect pop-up window
  • I create a robot to press the Enter button to close this pop-up, so close the whole browser. Here I have this error Error Message (Is it a focus problem?)
  • I would like to open the portal again and connect with another login

I have tried to catch this Error Message, but I don't understand why it's still crashing, and it doesn't enter in the catch..

        driver.findElement(By.id("disconnectBtn")).click();

        try {
            System.out.println("Entering in the first try");
            Robot r = new Robot();
            r.keyPress(KeyEvent.VK_ENTER);
        } catch (org.openqa.selenium.UnhandledAlertException f) {
            try {
                System.out.println("Entering in the second try");
                Alert alert = driver.switchTo().alert();
                String alertText = alert.getText();
                System.out.println("Alert data: " + alertText);
                alert.accept();
                Robot r = new Robot();
                r.keyPress(KeyEvent.VK_ENTER);
            } catch (NoAlertPresentException e) {
                System.out.println("Entering in the second catch");
                e.printStackTrace();
            }
        }

Thank you in advance.

Mattéo
  • 11
  • 2
  • Is the pop up a javascript alert? If so the best practice would be to `driver.switchTo().alert().accept();` – Harry King Apr 17 '18 at 14:34
  • I have already tried this, it tells me no alert is detected. I can close the disconnect pop-up by pressing "Enter" with a robot. See the pop up on this link https://www.noelshack.com/2018-16-2-1523976283-capture.png – Mattéo Apr 17 '18 at 14:45
  • 2
    It would be good if you can write this questions in a steps. Please share the code you have written and error trace along with relevant html? – cruisepandey Apr 17 '18 at 15:35
  • I have updated my post, thank you for helping. – Mattéo Apr 18 '18 at 06:50

1 Answers1

0

If two different credential sets are used in single test case, better to open new anon window for the second login:

Robot rob = new Robot();                          
rob.keyPress(KeyEvent.VK_CONTROL); 
rob.keyPress(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_P); 
rob.keyRelease(KeyEvent.VK_CONTROL);
rob.keyRelease(KeyEvent.VK_SHIFT);
rob.keyRelease(KeyEvent.VK_P);
ArrayList<String> windows = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(windows.get(1));
pburgr
  • 1,722
  • 1
  • 11
  • 26