Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
590 views
by Contributing Tester (91 points)
retagged by

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 like 0 dislike
by Expert (572 points)
selected by
 
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.

by Contributing Tester (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
by Expert (572 points)
reshown by
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" ?
by Contributing Tester (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.
by Expert (572 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.
by Contributing Tester (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?
by Expert (572 points)
Please remove those. I just let you keep it earlier to not to confuse you.
by Contributing Tester (91 points)
But that is not making any difference. By keeping @Test annotation in my LoginPage and CreateTask classes not giving me any exception
by Expert (572 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.
by Contributing Tester (91 points)
Got it! Thanks a lot Sunil for all your help


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...