1

I have to create a common class which setup the selenium webdriver. My setup base class :Setupbase.java

public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://example.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}

This setup class is common. Whenever I write a new program I need to call this class. This is my login program: Login.java

public class Login extends Setupbase{
super.setUp();
driver.get(baseUrl + "/");
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("username");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();}

But i'm getting error while executing this code. can anyone help me regarding this.

2 Answers 2

1

This will be your setup class :

public class Setupbase {

WebDriver driver;
String baseUrl;
public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://example.com";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}

}

The class that uses that setup class:

public class Login extends Setupbase
{
@Test

public void LoginTest() throws Exception{

    super.setUp();
    driver.get(baseUrl + "/");
    driver.findElement(By.id("Email")).clear();
    driver.findElement(By.id("Email")).sendKeys("username");
    driver.findElement(By.id("Passwd")).clear();
    driver.findElement(By.id("Passwd")).sendKeys("password");
    driver.findElement(By.id("signIn")).click();}

}
Sign up to request clarification or add additional context in comments.

2 Comments

driver.get(baseUrl + "/"); from this line its not working.. empty webdriver window has been opened.. but the baseurl is not loading..
either dont append '/' in your base url string or delete '/' from your baseurl string in setupbase class.See the edited answer.
1

I would like to represent my structure I use in my project. It seems you forgot @Before, @After and @Test notation.

public class BaseSeleniumTest extends SeleneseTestBase {
    static WebDriver driver;

    @Before
    public void openFirefox() throws IOException {


        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        driver.get(propertyKeysLoader("login.base.url"));
        doAdminLogin();
    }


    @After
    public void closeFirefox(){
        driver.quit();
    }

    public void doAdminLogin() throws IOException {
        String curTitle=driver.getTitle();
        locatorFindingHandling("login.logininput", "login.admin.login");
        locatorFindingHandling("login.passinput", "login.admin.pass");
        locatorFindingHandling("login.loginbutton");

        String newTitle=driver.getTitle();
        Assert.assertFalse(curTitle.equals(newTitle));

    }


    public void locatorFindingHandling(String key) throws IOException /*throws IOException*/ {

        fluentWait(By.cssSelector(propertyKeysLoader(key))).click();

    }
    public void locatorFindingHandling(String key, String key1) throws IOException {

        driver.findElement(By.xpath(propertyKeysLoader(key))).sendKeys(propertyKeysLoader(key1));

    }

    public void doLogout() throws InterruptedException, IOException {
        String curTitle=driver.getTitle();
        jsClick("rms.home.logout");
        String newTitle=driver.getTitle();
        Assert.assertFalse(curTitle.equals(newTitle));

    }
....
}

And then I extend my BaseSeleniumTest.java in the following way:

public class LoginPageTestSuite extends BaseSeleniumTest {


    @Test
    public void loginWithEmptyCredentials() throws IOException, InterruptedException {
        doLogout();
        fluentWait(By.cssSelector(propertyKeysLoader("login.loginbutton"))).click();

        Assert.assertTrue(fluentWait(By.cssSelector(propertyKeysLoader("login.validator.invalidautentication"))).getText().trim().equals("Invalid authentication"));
    }

    @Test
    public void logoutAdminLogin() throws IOException, InterruptedException {
        doLogout();
        doAdminLogin();

    }

    @Test
    public void loginWithWrongPass() throws IOException, InterruptedException {
        doLogout();
        locatorFindingHandling("login.logininput", "login.admin.login");

        locatorFindingHandling("login.passinput", "login.invalidPass");

        locatorFindingHandling("login.loginbutton");
        Assert.assertTrue(fluentWait(By.cssSelector(propertyKeysLoader("login.validator.invalidautentication"))).getText().trim().equals("Invalid authentication"));

    }
.....
}

So from the point of your code it be something like:

   public class Setupbase extends SeleneseTestBase {
        static WebDriver driver;

        @Before
        public void openFirefox() throws IOException {        

            driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            String baseUrl = "http://example.com";
            driver.get(baseUrl);                
        }       

        @After
        public void closeFirefox(){
            driver.quit();
        }
}   

public class Login extends Setupbase{

@Test
public void loginTest() {
    driver.findElement(By.id("Email")).clear();
    driver.findElement(By.id("Email")).sendKeys("username");
    driver.findElement(By.id("Passwd")).clear();
    driver.findElement(By.id("Passwd")).sendKeys("password");
    driver.findElement(By.id("signIn")).click();
  }
}

Hope this works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.