1

I had written my first Page Object Pattern. Can anybody to check this code and tell me the feedback? What mistakes are that i have made? I want to correct it? I am Verifying to access login page and Entered values in client registration module. Here is my code...

package com.qber.Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
public class LoginPage
{
    WebDriver driver;
    WebElement element;
By user=By.name("username");
By pass=By.name("passsword");
By button=By.className("button");
By admin=By.linkText("Admin");
By client=By.linkText("Clients");
public LoginPage(WebDriver driver)
{
    this.driver=driver;

}
public void typeUser()
{
    WebElement element=driver.findElement(By.name("username"));
    element.sendKeys("");

}
public void typePass()
{
    element=driver.findElement(By.name("password"));
    element.sendKeys("");
}
public void typeButton()
{
    element.submit();
}
public void typeAdmin()
{
    element=driver.findElement(By.linkText("Admin"));
    element.click();    
}
public void typeClients()
{
    element=driver.findElement(By.linkText("Clients"));
    element.click();
     element= driver.findElement(By.xpath("//*[@id='search-container']/a/i"));
     element.click();
    //check whether all fields are blank
     try
     {
         WebElement element1=driver.findElement(By.xpath("//*[@id='name']"));
     element1.submit();
     System.out.println("name is mandatory");
     }
     catch(Exception e)
     {
         System.out.println("incorrect"+e.getMessage());
     }
}
     //check response for entering special character
     public void addclient()
     {
     try
     {
         WebElement element1=driver.findElement(By.id("name"));
         element1.sendKeys("Shaik");
         element1=driver.findElement(By.id("group_name"));
         element1.sendKeys("sk123");
         element1=driver.findElement(By.name("address"));
         element1.sendKeys("Uae");
         Select stat=new Select(driver.findElement(By.id("status")));
         stat.selectByVisibleText("active");
         element1.submit();
        System.out.println("Test passed");

     }
     catch(Exception e)
     {
         System.out.println("incorrect"+e.getMessage());
     }


}
     public void invalidClient()
     {
        try
        {
            WebElement element1=driver.findElement(By.id("name"));
            element1.sendKeys("@##");
            element1=driver.findElement(By.id("group_name"));
            element1.sendKeys("%$$%^$");
            element1=driver.findElement(By.name("address"));
            element1.sendKeys("#%$#%$");
            Select stat=new Select(driver.findElement(By.id("status")));
            stat.selectByVisibleText("inactive");
            element1.submit();
           System.out.println("Status is mandotory: Test passed");
        }
        catch(Exception e)
        {
            System.out.println("incorrect"+e.getMessage());
        }
     }
}


package com.qber.Testcases;
import org.testng.annotations.Test;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.qber.Pages.LoginPage;
@Test
public class verifyLogin {

    public void validLogin()
    {
    System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");
    FirefoxDriver driver=new FirefoxDriver();


    driver.get("http://54.68.159.204/qmsadm");
    driver.manage().window().maximize();

    LoginPage login=new LoginPage(driver);
    login.typePass();
    login.typeUser();
    login.typeButton();
    login.typeAdmin();
    login.typeClients();
    login.addclient();
    login.invalidClient();
    }




}

closed as unclear what you're asking by IAmMilinPatel, NarendraC, Bharat Mane, ECiurleo, Kate Paulk Sep 26 '16 at 11:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I think you should reword the question in order to be non-opinionated. – Paul Muir Sep 22 '16 at 18:48
  • Are you facing any issues with this code? – IAmMilinPatel Sep 23 '16 at 4:03
  • @TESTasy.. I am in stuck. I want to correct it, then only I can go forward otherwise I am in stuck that is my probelm. What is your opinion should I go forward or to try again to correct my code? – user21268 Sep 23 '16 at 6:01
  • Stuck where? Which part is not working? Are you getting any error? If yes, what error? Can you share the error (maybe a screenshot)? – IAmMilinPatel Sep 23 '16 at 6:03
  • @TESTasy There is no error. Is it okay? – user21268 Sep 23 '16 at 6:05
1

The basics of the PageObject model is that the Page is an object which can be modified without having to make changes to each test.

It would appear to me that you are attempting to combine Tests with Page Object. In order to stick closer to a Page Object Model you should remove all business logic from the object itself and let the object just manage the page itself.

Also, a good practice is typically to use the PageFactory. This will lazy load the elements on the page and only grab them when they are needed. By doing this when you instantiate the object it will have all of the Properties that are needed to be initialized.

An example would be:

public void typeUser()
{
    WebElement element=driver.findElement(By.name("username"));
    element.sendKeys("geosony");

}

Should be:

@FindBy(how = How.NAME, using = "username")
public WebElement userName;
public void typeUser(string Username)
{
    userName.sendKeys(Username);
}

By doing it this way, in your tests you can use object.typeUser("username"); and it will work. If there is a change to the username element, all you have to do is update the element in one place and all of the tests are then updated.

Also, you are going to want to split each different page into a different object. I typically have mine inherit from a single object which does all of the initialization methods. By doing this if something has to change in the instantiation process it also gives you the ability to update all objects simultaneously.