Question

I'm new to Selenium Webdriver, I have a few question :

First, I have 2 class, 'Login.java' and 'SelectCity.java

Class 1 : Login.java

  • invalidLogin

  • validLogin

Class 2 : SelectCity.java

For now, in Login.java I wrote

@After

public void tearDown() throws Exception {
    driver.close();
}

which mean the browser will closed after finish run right?

Here is my question :

  1. How to make after I run validLogin, it will continue to run the next class (SelectCity.java)? Is it possible to do that?

  2. How to make last browser (which test valid login) didn't get close?

My current Login.java class :

      public class Login  extends SelectCityAfterLogin{

  private WebDriver driver;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();

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


  @Test 
  public void testLoginInvalidPass() throws Exception {
    driver.get(baseUrl + "/mysite/login.aspx");
    driver.findElement(By.id("txtUsername")).sendKeys("user1");
    driver.findElement(By.id("txtPassword")).sendKeys("somePassword");
    driver.findElement(By.id("lbLogin")).click();

    try {
      assertEquals("Invalid User or Password", driver.findElement(By.id("lblError")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @Test 
  public void testLoginInvalidUsername() throws Exception {
    driver.get(baseUrl + "/mysite/login.aspx");
    driver.findElement(By.id("txtUsername")).sendKeys("username");
    driver.findElement(By.id("txtPassword")).sendKeys("somepassword");
    driver.findElement(By.id("lbLogin")).click();

    try {
      assertEquals("Invalid User or Password", driver.findElement(By.id("lblError")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @Test 
  public void testLoginNoUsername() throws Exception {
    driver.get(baseUrl + "/mysite/login.aspx");
    driver.findElement(By.id("txtUsername")).sendKeys("");
    driver.findElement(By.id("txtPassword")).sendKeys("somePassword");
    driver.findElement(By.id("lbLogin")).click();

    try {
      assertEquals("Please fill username and password.", driver.findElement(By.id("lblError")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @Test 
  public void testLoginNoPassword() throws Exception {
    driver.get(baseUrl + "/mysite/login.aspx");
    driver.findElement(By.id("txtUsername")).sendKeys("username");
    driver.findElement(By.id("txtPassword")).sendKeys("");
    driver.findElement(By.id("lbLogin")).click();

    try {
      assertEquals("Please fill username and password.", driver.findElement(By.id("lblError")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @Test 
    public void testLoginValid() throws Exception{
    //SelectRoleAfterLogin selectRole = SelectRoleAfterLogin();  

    driver.get(baseUrl + "/mysite/login.aspx");
    driver.findElement(By.id("txtUsername")).sendKeys("myUsername");
    driver.findElement(By.id("txtPassword")).sendKeys("password");
    driver.findElement(By.id("lbLogin")).click();
    Thread.sleep(3000);     
  }  

}

Thx

Was it helpful?

Solution

Yes you can run selectcity after login

You should Extend your First class to Second class

In your Login.java extend it to selectcity java like below

Public class login extends Selectcity {

After your validlogin

create a new instance for selectcity (If you are using @test, create the below in that)

selectcity test = new selectcity()
Once after your valid login do the following
test.(will fetch you the methods available in selectcity.java)

By this way you can call once class to another.

Let me know if this helps

UPDATE :

if selectcityAfterlogin in public class Login extends **SelectCityAfterLogin** is your second class name, then you are creating an incorrect object i guess

In your public login class please check whether your are extending the proper second class name

In @test method

@Test 
    public void testLoginValid() throws Exception{
    //SelectRoleAfterLogin selectRole = SelectRoleAfterLogin(); 

it should be

SelectCityAfterLogin selectrole = new SelectCityAfterLogin()
selectrole.(will fetch you the methods of second city)

Please let me know if this helps.

One better solution would be to create multiple methods than multiple @test. You can call these to single @test suite.

OTHER TIPS

I am assuming, you have 2 classes 'Login.java' and 'SelectCity.java' as follows:

Class 1 : Login.java

public void invalidLogin (WebDriver driver){

//some operations

}

public void validLogin (WebDriver driver){

//some operations

}

Class 2 : SelectCity.java

public void selectCity (Webdriver Driver){

//some operations

}

Class 3: LoginAndSelectCity.java

public static void main(String args []){

WebDriver driver = new FirefoxDriver();

Login.validLogin(driver);
SelectCity.selectCity(driver);

}

If all classes should be in same package then only it works else you need to import packages or create new child to do inheritance of the classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top