【问题标题】:How to write a code that will execute after each step is executed如何编写将在每个步骤执行后执行的代码
【发布时间】:2016-06-12 00:33:03
【问题描述】:

我正在使用的应用程序会引发很多意外警报。 我想实现一种通过通用方法 isAlert present 捕获所有这些的方法。

但是如何为 webdriver 中的每一步调用 isAlertpresnt ,有人可以在这里帮助我吗?

通常我的方法是这样的:-

public void checkAlert() {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 2);
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
    } catch (Exception e) {
        //exception handling
    }
}

问题是如何在每一步之前调用它?我知道这会使我的代码变慢,但我非常希望实现这一点。

我正在寻找在我的测试中的每个命令\步骤之后执行的东西。这可能吗?

目前我在所有预期场景中都使用 try catch 调用此方法。

【问题讨论】:

  • 您是否使用任何测试框架(例如junittestng)来运行测试?您可以查看可用的annotations
  • 我正在使用 TestNG 框架。但是我看不到在每个命令之后执行的任何匹配注释

标签: java selenium frameworks webdriver alerts


【解决方案1】:

使用事件监听器已经提供了一个很好的答案。

另一种简单的处理方法,如果您对所有 selenium 操作使用关键字/方法。我的意思是,如果您使用 like click("locator") 方法在测试用例中执行单击而不是一次又一次地编写驱动程序命令,那么您可以在单击该单击方法后插入该警报交叉检查命令.

 public void myClick(String myxpath){

    driver.findElement(By.xpath(myxpath)).click();
    //calling checkAlert method to cross check
}

因此,如果您使用单击、输入等方法进行 selenium 操作,那么您可以像上面那样尝试。

谢谢你, 壁画

【讨论】:

  • 谢谢 Murali,我会在我的下一个框架中尝试这个。我想我们之前已经谈过硒训练 :)
  • hoo..对不起,我不记得之前的讨论,如果发生,堆栈是如何获得帮助的非常好的平台,我也会尽力而为..
【解决方案2】:

WebDriver 有 WebDriverEventListener 监听器,用于您正在尝试做的事情。通过实现此侦听器并注册驱动程序 - 您可以在您将使用 webdriver 执行的每个操作之前/之后在后台调用此 checkAlert 方法。

看看这个例子。

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

【讨论】:

    【解决方案3】:
    I could think of a solution using a Thread, which always monitors if there is any alert present, if yes then accept else don't do any thing. Considering you are using a testNG or Junit framework, here is the sample:
    
    
    package poc.grid;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoAlertPresentException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    
    public class Test {
    
        static WebDriver driver;
    
    //This method returns a Thread, which monitors any alert and accept whenever finds it. And this return a Thread object.
    public static Thread handleAlert(final WebDriver driver)
        {
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    while(true)
                    {
                        try
                        {
                            System.out.println("Checking alert .... ");
                            driver.switchTo().alert().accept();
                            System.out.println("Alert Accepted. ");
                        }catch(NoAlertPresentException n){
                            System.out.println("No Alert Present.  ");
                        }catch (Exception e) {
                            System.out.println("Exception: "+e.getMessage());
                        }
                    }
                }
            });
    
            return thread;
        }
    
        @BeforeTest
        public void beforeTest()
        {
            driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    
            //In before Test, just call Thread and start it.
            handleAlert(driver).start();
        }
    
        //This is your normal Test
        @org.testng.annotations.Test
        public static void test() 
        {
            try
            {
                driver.get("https://payments.billdesk.com/pb/");
    
                int i=0;
                while(i<=10)
                {
                    driver.findElement(By.xpath("//button[@id='go']")).click();
                    Thread.sleep(2000);
    
                    i++;
                }
            }catch(Exception e)
            {
                System.out.println("Exception: "+e.getMessage());
            }
        }
    
        //At the end of test, you can stop the Thread.
        @AfterTest
        public void afterTest()
        {
            handleAlert(driver).stop();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-14
      • 2012-09-16
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2022-01-23
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多