Welcome to the Question and Answer site for Software Testing Professionals

You can get your queries resolved from Software Testing Professionals.

Ask a Question

Get answers and give answers to questions from other members.

View All Questions

Get votes and earn points to build your reputation in community.

View Hot Questions

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

Solved 1 Answers 42 Views
1 1

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
1

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, 2017 by SunilBhaskar (443 points)
selected Sep 12, 2017 by stbadmin
9Comments
commented Sep 12, 2017 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, 2017 by SunilBhaskar (443 points)
reshown Sep 12, 2017 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, 2017 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, 2017 by SunilBhaskar (443 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, 2017 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, 2017 by SunilBhaskar (443 points)
Please remove those. I just let you keep it earlier to not to confuse you.
commented Sep 12, 2017 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, 2017 by SunilBhaskar (443 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, 2017 by Priyanka91 (91 points)
Got it! Thanks a lot Sunil for all your help

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

We have resolved 1.2k+ Software Testing queries!

Software Testing Board Weekly update

We will send you handpicked questions that you would love to answer.

View previous campaigns.

Top Contributors

...