How to write a code that will execute after each s

2019-08-13 03:18发布

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.

3条回答
Root(大扎)
2楼-- · 2019-08-13 03:27

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楼-- · 2019-08-13 03:39

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/

查看更多
Rolldiameter
4楼-- · 2019-08-13 03:40
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();
    }

}
查看更多
登录 后发表回答