Home > Software engineering >  Web UI automation for a pop-up confirmation window
Web UI automation for a pop-up confirmation window

Time:11-16

This is not a code problem as much as it is an automation problem.

There is a desktop website made of iframes(to me looks like those are iframes) which is some kind of B2B shop. All website elements are fixed in place, they are not moving or changing when moving from screen to screen.

What I need is to automatize a few clicks on buttons, until I come to confirmation, one of those clicks is one pop-up window with Confirm/Cancel. After this pop-up, the next screen is opened and one more button to click, which is manual work for me.

I have tried using a few QA testing software such as Selenium IDE to automatize, but it is not helping, when I come to that pop-up it just stops. To me looks like the software is not aware of that pop-up overlay, even though that pop-up is always the same.

Does anyone have any idea how to overcome the above problem?

Thanks everyone in advance.

Important: Pop-up window is part of website.

CodePudding user response:

If the OS is Windows, and the popup is a native Windows dialog - and not part of the website accessible by Selenium IDE - you could try solving your problem by using calls to Windows API. Not sure how if this can be done using selenium.ide, but it sure can be achieved using regular Selenium Webdriver code written in one of the supported programming languages that are able to execute native Windows functions.

Here, for example, is a stand-alone Java class that starts Chrome, navigates to a website and then clicks on OK button displayed in the native dialog called "Warning".

package example;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
import org.openqa.selenium.chrome.ChromeDriver;

import java.time.Duration;

public class Main {
    public static void main(String... args) {
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://google.com");

        Pointer dialog = getNativeDialog("Warning");
        Pointer button = getNativeButton(dialog, "OK");
        click(button);
    }

    /**
     * Returns a handle to a dialog window by its name
     * @param dialogTitle exact name of the window to find
     */
    public static Pointer getNativeDialog(String dialogTitle) {
        final String DIALOG_CLASS = "#32770";
        return User32.INSTANCE.FindWindowEx(null, null, DIALOG_CLASS, dialogTitle);
    }

    /**
     * Returns a handle to the button with specific label within a parentWindow
     * @param parentWindow handle to a window, obtained e.g. with {@link #getNativeDialog(String)}
     * @param buttonLabel label of the button to search by
     */
    public static Pointer getNativeButton(Pointer parentWindow, String buttonLabel) {
        final String BUTTON_CLASS = "Button";
        return User32.INSTANCE.FindWindowEx(parentWindow, null, BUTTON_CLASS, buttonLabel);
    }

    /**
     * Sends BM_CLICK message to a window (or other control) which is equivalent of clicking it
     * with primary mouse button
     * @param target an input handle obtained with e.g. {@link #getNativeButton(Pointer, String)}
     */
    public static void click(Pointer target) {
        final int BM_CLICK = 0x00F5;
        User32.INSTANCE.SendMessage(target, BM_CLICK, null, null);
        sleep(Duration.ofMillis(500));
    }


    private static void sleep(Duration duration) {
        try {
            Thread.sleep(duration.toMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);

        /**
         * Finds window (or other type of control, specified by lpszClass).
         * See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowexw
         */
        Pointer FindWindowEx(Pointer hWndParent, Pointer hWndChildAfter, String lpszClass, String lpszWindow);

        /**
         * Sends message to a window (or other type of control). Can be used to simulate user input.
         * Note when the result of sending message triggers modal dialog to show, this method may block indefinitely,
         * unless the dialog is handled in separate thread.
         * See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage
         */
        Pointer SendMessage(Pointer hWnd, int Msg, String wParam, String lParam);
    }
}

This particular example requires these libraries

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>platform</artifactId>
            <version>3.4.0</version>
        </dependency>
    </dependencies>
  • Related