如何编写在每个步骤执行后执行的代码

How to write a code that will execute after each step is executed

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

但是如何在 webdrriver 中为每个步骤调用 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
    }
}

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

我正在寻找在我的测试中每次 command\step 之后执行的东西。这可能吗?

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

WebDriver 有 WebDriverEventListener 个侦听器,可以准确地了解您正在尝试执行的操作。通过实施此侦听器并注册驱动程序 - 您可以在幕后调用此 checkAlert 方法 before/after 您将使用 webdriver 执行的每个操作。

看看这个例子。

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

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();
    }

}

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

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

 public void myClick(String myxpath){

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

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

谢谢, 穆拉利