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

浪尽此生 提交于 2019-12-13 02:57:52

问题


The application i am working throws a lot of unexpected alerts . I wanted to implement a way that catch all these through a common method isAlert present .

But how to call isAlertpresnt for each step in webdrriver , can anyone help me here ?

Typically my method would be like this :-

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
    }
}

The issue is how to call it before every step ? I know this will make my code slow but badly want this to be implemented .

I am looking for something which executes after each command\step in my test .Is this possible ?

Currently i am calling this method in all expected scenarios with a try catch.


回答1:


WebDriver has WebDriverEventListener listener for what you are trying to do exactly. By implementing this listener and registering the driver - you can get this checkAlert method called behind the scenes before/after every action you will perform using webdriver.

Take a look at this example.

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




回答2:


Already one good answer is provided as by using event-listener.

Another simple way to handle, if your are using keywords/methods for all selenium actions. What i meant to say is, if you are using like click("locator") method to perform click in your test cases instead of writing driver command again and again, then you can insert that alert cross checking command after click in that click method.

 public void myClick(String myxpath){

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

so if you are using methods like click, input etc for selenium actions, then you can try like above.

Thank You, Murali




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

}


来源:https://stackoverflow.com/questions/35691603/how-to-write-a-code-that-will-execute-after-each-step-is-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!