【问题标题】:Implement global listener to pause code execution实现全局监听器以暂停代码执行
【发布时间】:2021-04-29 10:25:38
【问题描述】:

我有这段代码用于暂停 Selenium 代码的执行:

Boolean waitruntil = new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='ngx-loading-text center-center' and starts-with(., 'Loading')]")));

我将每个测试都运行到 JUnit 代码中。你知道我如何实现一个全局监听器,如果显示加载栏,它会暂停代码执行(使用 Xpath 定位器检测)

【问题讨论】:

    标签: selenium selenium-webdriver junit selenium-chromedriver junit5


    【解决方案1】:

    一种比使用监听器复杂一点的解决方案是使用代理包装您的元素,并在您点击代理时执行检查。

    让监听器监听事件而不改变测试的流程,

    首先,我们需要创建一个类,它会在您调用元素时执行检查:

    public class ElementProxy implements InvocationHandler {
    
        private final WebElement element;
    
        public ElementProxy(WebElement element) {
            this.element = element;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            this.waitForPageLoad();
            return method.invoke(element, args);
        }
    
        private void waitForPageLoad() {
            WebDriver driver = ((WrapsDriver) element).getWrappedDriver();
            WebElement loadingBar = driver.findElement(By.xpath("//div[@class='ngx-loading-text center-center' and starts-with(., 'Loading')]"));
            new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElement(loadingBar))
        }
    
        public static WebElement proxy(WebElement element) {
            ElementProxy proxy = new ElementProxy(element);
            WebElement wrappdElement = (WebElement) Proxy.newProxyInstance(ElementProxy.class.getClassLoader(),
                                                                           new Class[] { WebElement.class },
                                                                           proxy);
            return wrappdElement;
        }
        
    }
    

    然后您可以调用ElementProxy.proxy(element),这将返回一个新的代理元素,类似于PageFactory 在幕后的行为。 最后一件事是将它包裹在你自己的PageFactory 下,这样你就可以控制元素的行为:

    public class MyPageFactory {
        
        public static <T> void initElements(WebDriver driver, T pageObject) {
            PageFactory.initElements(driver, pageObject);
            
            for (Field field : pageObject.getClass().getDeclaredFields()) {
                if (field.getType().equals(WebElement.class)) {
                    boolean accessible = field.isAccessible();
                    field.setAccessible(true);
    
                    field.set(pageobject, ElementProxy.proxy((WebElement) field.get(pageObject)));
                    field.setAccessible(accessible);
                }  
            }
    
        }
        
    }
    

    现在,如果您在存在加载栏的网页中,请使用新的MyPageFactory,它将自动执行检查。

    如果您不需要执行检查,请使用常规的PageFactory

    参考:https://www.vinsguru.com/selenium-webdriver-how-to-handle-annoying-random-popup-alerts/

    【讨论】:

    • 我还有一个问题要问。我有一系列按钮可以点击。在那种情况下,我该如何使用此代码?
    【解决方案2】:

    有一个WebDriverEventListener可以完成你想要的。您可以实现此侦听器,以便在 selenium 执行每个命令之前,侦听器检查visibilityOfElementLocated(By.xpath("//div[@class='ngx-loading-text center-center' and starts-with(., 'Loading')]")));,如果是,则执行您的 WebDriverWait。在没有看到您的代码的情况下,我不确定您将如何实现它,但这里有一些很好的示例资源。

    http://toolsqa.com/selenium-webdriver/event-listener/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多