dimanche 7 juin 2015

Selenium alert handling testing approach/ Design pattern

I am new to Selenium and I am trying typical login page automation and came across below scenario and I want to know the professional approach to handle the situation.

Scenario Test application login page using valid & Invalid Credentials. When I enter invalid credentials, a pop up comes and inform the user that you have entered wrong user name or password.

My Approach

  1. I have created a page-object class to hold all Web elements using Page Factory and exposed Web elements to outside class using some public methods.

  2. In the test class I have created objects of the page under test and passed login credentials using testng xml.

Then I ran Testng suit and it ran fine. but the problem is when I pass invalid credentials a popup window is coming and since I have not coded for popuup window I am getting exception.

I know what to do for popup but my real question is should I write a different test class to handle invalid credentials. or my Question how can i write a common code for two different output ie when I enter valid credentials it goes to HomePage and when I enter Invalid i get popuup and I will be in Login which is actually excepted and my test-case should pass.

Login.java

public void loginToGuru99(String userName,String password){
    this.setUsername(userName);
    this.setPassword(password);
    this.clickOnLogin();
}

TestGuru99LoginPage.java

  @BeforeTest
  public void setup() {
        driver= new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://ift.tt/1yR5kVA");
  }

// @Test(dataProvider="Authentication")


  @Test
  @Parameters({ "sUsername", "sPassword" })
  public void TestGuru99LoginPage(String sUsername,String sPassword) {
      login= new Guru99LoginPage(driver);
      homePage= new Guru99HomePage(driver);
      try{
          login.loginToGuru99(sUsername, sPassword);          
          Assert.assertTrue(homePage.getHomePageDashboardUserName().toLowerCase().contains("manger id : mngr13564"));
      }catch(Exception e){
          e.printStackTrace();
      }
  }

Utils.java

public static boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  public static String closeAlertAndGetItsText() {
        try {
          Alert alert = driver.switchTo().alert();
          String alertText = alert.getText();
          if (acceptNextAlert) {
            alert.accept();
          } else {
            alert.dismiss();
          }
          return alertText;
        } finally {
          acceptNextAlert = true;
        }
      }

Please let me know the best approach to start with.

Thanks, Deepu Nair

Aucun commentaire:

Enregistrer un commentaire