简体   繁体   English

弹出确认窗口的 Web UI 自动化

[英]Web UI automation for a pop-up confirmation window

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.有一个由 iframe 组成的桌面网站(对我来说看起来像是 iframe),这是某种 B2B 商店。 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.我曾尝试使用一些 QA 测试软件(例如 Selenium IDE)来实现自动化,但它没有帮助,当我出现该弹出窗口时,它就停止了。 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.重要提示:弹出窗口是网站的一部分。

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.如果操作系统是 Windows,并且弹出窗口是本机 Windows 对话框 - 而不是 Selenium IDE 可访问的网站的一部分 - 您可以尝试通过调用 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.不确定如何使用 selenium.ide 来实现这一点,但肯定可以使用以能够执行本机 Windows 功能的受支持编程语言之一编写的常规 Selenium Webdriver 代码来实现。

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".例如,这里是一个独立的 Java 类,它启动 Chrome,导航到一个网站,然后单击显示在名为“警告”的本机对话框中的“确定”按钮。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM