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

3

1

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.

Priority answered 29/2, 2016 at 3:42 Comment(2)
Are you using any test framework such as junit or testng to run the tests? You may look at available annotations.Hulton
I am using TestNG framework . But i Could not see any matching annotations which execute after each commandPriority
O
2

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/

Oxford answered 29/2, 2016 at 4:32 Comment(0)
O
1
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();
    }

}
Oriflamme answered 29/2, 2016 at 5:53 Comment(0)
C
1

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

Caucasus answered 29/2, 2016 at 7:19 Comment(2)
Thanks Murali , I would try this in my next framework .I think we have spoken before on selenium training :)Priority
hoo..sorry, i am not remember previous discussion if happens, any how stack is very good platform to get helps and also i will try best..Caucasus

© 2022 - 2024 — McMap. All rights reserved.