Do you have questions ? We got the answers!

Ask a question:

1
How to link the different classes in different .java file?

Solved 1 Answers 32 Views

Login.java file:

public class LoginPage

{

@Test

public void loginAs() throws Exception {

String baseUrl = "";

String uname = "";

String pwd = "";

 

driver = new FirefoxDriver();

driver.get(baseUrl);

String url = "";

 

driver.manage().window().maximize();

 

driver.findElement(By.xpath("//*[@id=\"login-form\"]/tbody/tr[2]/td/div[1]/input")).sendKeys(uname);

driver.findElement(By.xpath("//*[@id=\"login-form\"]/tbody/tr[2]/td/div[2]/input")).sendKeys(pwd);

driver.findElement(By.id("login")).click();

String currentUrl = driver.getCurrentUrl();

if(currentUrl.equals(url)) {

System.out.println("Login Success");

}

else {

System.out.println("Login Fail");

 

}

driver.navigate().to("");

}

}

CreateTask.java file:

public class CreateTask {

WebDriver driver;

@After

public void tearDown() throws Exception {

}

@Test

public void addTask() {

driver.findElement(By.xpath("//*[@id=\"my-workspace-box\"]/ul/li[1]/a[1]")).click();

}

}

MainMethod.java File

public class firefoxDemo {

public static void main(String[] args) {

//System.setProperty("webdriver.gecko.driver", "C:\\Users\\praviprakash\\Desktop\\geckodriver-v0.18.0-win64.exe");

 

LoginPage login = new LoginPage();

CreateTask task = new CreateTask();

try {

login.loginAs();

task.addTask();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Above is the code. Application is not running after the login functionality. How should i link so that it will run continously?

1 Answer

1
Best answer

You are initiating your driver on Login, so it will open the driver. But, you have not initiated your driver in CreateTask.java

I suggest you rewrite it as given below.

 

public class LoginPage

{
WebDriver driver;

public LoginPage(WebDriver driver){
this.driver = driver;
}


public void loginAs() throws Exception {

String baseUrl = "";

String uname = "";

String pwd = "";

 

driver.get(baseUrl);

String url = "";

 

driver.manage().window().maximize();

 

driver.findElement(By.xpath("//*[@id=\"login-form\"]/tbody/tr[2]/td/div[1]/input")).sendKeys(uname);

driver.findElement(By.xpath("//*[@id=\"login-form\"]/tbody/tr[2]/td/div[2]/input")).sendKeys(pwd);

driver.findElement(By.id("login")).click();

String currentUrl = driver.getCurrentUrl();

if(currentUrl.equals(url)) {

System.out.println("Login Success");

}

else {

System.out.println("Login Fail");

 

}

driver.navigate().to("");

}

}

public class CreateTask {

WebDriver driver;

public CreateTask(WebDriver driver){
this.driver = driver;
}

@Test

public void addTask() {

driver.findElement(By.xpath("//*[@id=\"my-workspace-box\"]/ul/li[1]/a[1]")).click();

}

}

public class firefoxDemo {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

 

LoginPage login = new LoginPage(driver);

CreateTask task = new CreateTask(driver);

try {

login.loginAs();

task.addTask();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

You need to initiate driver and pass it to respective class. Also, I have removed @Test and other annotations as there is no use of it here, as far as you have main method invoking other classes.

answered Sep 7 by SunilBhaskar (433 points)
selected Sep 12 by stbadmin
9Comments
commented Sep 12 by Priyanka91 (91 points)
Hi Sunil,

Thanks for the reply!

Its working after making the changes that you mentioned above, but program is getting terminated after every run. Have to call the functions every time in Main method to run the program each time. Can you please help with this
commented Sep 12 by SunilBhaskar (433 points)
reshown Sep 12 by SunilBhaskar
I am unclear on what your requirement is. What do you mean by "program is getting terminated after every run. Have to call the functions every time in Main method to run the program each time" ?
commented Sep 12 by Priyanka91 (91 points)
Getting this Exception "java.lang.Exception: Test class should have exactly one public zero-argument constructor" for the above code . Program is running successfully with a run time exception. Can you please look on this.
commented Sep 12 by SunilBhaskar (433 points)
There are two different ways to execute this code.
1. Using public static void main method.
2. Using @Test annotation using TestNG or JUnit

The solution that I gave you was to use your code with the Main method.

To execute tests using @Test method, you will have to go for different class structure.
e.g. replace your Main method in FirefoxDemo like given below.



public class firefoxDemo {

@Test
public void myTest() {

WebDriver driver = new FirefoxDriver();

 

LoginPage login = new LoginPage(driver);

CreateTask task = new CreateTask(driver);

try {

login.loginAs();

task.addTask();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Now to explain you the exception, you are trying to start a test through a class where you have a constructor that requires a parameter. This will fail as JVM will not be able to initiate a test class. So, you are seeing the above error.
commented Sep 12 by Priyanka91 (91 points)
Thanks Sunil. It worked after adding @Test annotation instead of main method.

Can I keep @Test annotation in my LoginPage and CreateTask classes as well?
commented Sep 12 by SunilBhaskar (433 points)
Please remove those. I just let you keep it earlier to not to confuse you.
commented Sep 12 by Priyanka91 (91 points)
But that is not making any difference. By keeping @Test annotation in my LoginPage and CreateTask classes not giving me any exception
commented Sep 12 by SunilBhaskar (433 points)
You are right! It will not make any difference or give you any exception. But, there are two reasons to remove it.
1. It will make your code look neater. As unused annotation will drive questions in future.
2. The JVM will have to load those methods as Test methods and map it to TestNG/JUnit tests tree. It does not really affect your performance, but, it's good to not to have them as per coding standards.
commented Sep 12 by Priyanka91 (91 points)
Got it! Thanks a lot Sunil for all your help

Top Contributors

  • 4970 pointsstbadmin

    34 questions 454 answers218 comments

  • 433 pointsSunilBhaskar

    41 questions 15 answers32 comments

  • 425 pointsMehedi

    36 questions 3 answers14 comments

  • 363 pointssumitkumarwatts

    0 questions 13 answers0 comments

  • 354 pointsSoft Tester

    43 questions 0 answers4 comments

  • 327 pointsKeeshaGolden

    0 questions 17 answers2 comments

  • 322 pointsVipul Sharma

    44 questions 0 answers24 comments

  • 310 pointsvysh.sharma

    25 questions 0 answers0 comments

  • 267 pointsOnkar_Nimje

    33 questions 15 answers15 comments

  • 258 pointsVanitha

    26 questions 0 answers6 comments

...