bumblebee87
bumblebee87

Reputation: 81

selenium web driver: How to call a class from selenium Junit program

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.

Upvotes: 1

Views: 12415

Answers (2)

eugene.polschikov
eugene.polschikov

Reputation: 7339

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.

Upvotes: 1

Abhishek_Mishra
Abhishek_Mishra

Reputation: 4621

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();}

}

Upvotes: 1

Related Questions