I have a a method that is being called from Main but when the time comes to call the static method within it will not proceed and test stops.
I have inserted log comments in order to tell where the problem is and no exceptions are caught so there is no compiling nor runtime errors so far.
The static method that is not being called is GC2CommonMethods.loadApplication();.
The weird thing is that when running Main from Eclipse IDE it runs perfectly but it does not when executing from jar file through the same Main method.
See below both codes from the method that is being executed and the detail of the static method that is within a static class respectively.
I would appreciate your help with this. Thanks.
//This method is intented to be called from Main method
package com.mycompany.test.loginRoleEntitlements;
public void verifyLoginPageElements() {
logger.info("\t1.0/1.0.2 - Verif**strong text**ying Login page elements...");
try {
logger.info("entering Try");
GC2CommonMethods.loadApplication(sl); //Static method from Static class.
assertTrue("Region identifier is not present.", sl.isElementPresent(PageAttributes.LoginPage.DB_LABEL));
assertTrue("Forgot Password link is not present", sl.isElementPresent(PageAttributes.LoginPage.FORGOT_PASSWORD));
} catch (SeleniumException se) {
logger.info("caught SeleniumException");
logger.error(se.getMessage());
throw se;
} catch (AssertionFailedError ae) {
logger.info("caught AssertionException");
logger.error(ae.getMessage());
throw ae;
} catch (Exception e) {
logger.info("caught Exception");
logger.info("Encountered exception");
e.printStackTrace();
}
//This is the static method that is within GC2CommonMethods static class
package com.mycompay.common;
public static void loadApplication(SeleniumHandle sl) {
sl.open(props.getProperty("APPLICATION_URL"));
sl.waitForPageToLoad("30000");
assertEquals("The page is not the correct one.
|Expected: "+PageAttributes.LoginPage.LOGINPAGE_TITLE + ".
Found:"+sl.getTitle(),PageAttributes.LoginPage.LOGINPAGE_TITLE,sl.getTitle());
}
I found the solution. The problem was on the main method when trying to read a properties file. There was an uncaught exception which was not being logged so I was not able tell where the problem was. Thanks for your replies.
Related
I have the below code where I made a simple GUI. I would like Button2 to navigate to class 'Project2', which should start another piece of code. Just to note, in its current state, 'Project2' has no GUI, though I intend to add one soon. Anyway, this 'code jump' which I used by adding: String[] args = {};
Project2.main(args);
is not working, as the IDE says 'IOException must be caught or thrown'. I know how this works, though I am not sure how to implement it in the program.
Thanks in advance!
You can try to use dynamic class loading for your program. Below you can find lambda, which calls main method from com.stackoverflow.ExternalCaller class.
If you do not like to use lambda, you can create a simple anonymous class.
button.addActionListener(s -> {
try {
Class<?> externalCaller = Class.forName("com.stackoverflow.ExternalCaller");
Method main = externalCaller.getDeclaredMethod("main", new Class[]{String[].class});
main.invoke(null, new Object[]{new String[0]});
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
});
ExternalCaller class in its turn looks something like that:
package com.stackoverflow;
public class ExternalCaller {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
In result once you click on the button you will get Hello World output in console.
If you would like to work with external jars etc. please look on Process class. Quick example:
Process proc = Runtime.getRuntime().exec("java -jar External.jar");
Or even more on fork/exec. You can read From Runtime.exec() to ProcessBuilder for more details.
Hope this will help. Good luck.
In most of the IDE's, when you right-click on the Button2 in the Design(GUI) pane, you can travel through:
Events -> Actions -> actionPerformed().
And write this code in the selected method to switch classes:
this.setVisible(false); //turns off the visibility of the current class
outputClass out = new outputClass(); //creating the object of the class you want to redirect to
out.setVisible(true);//turns on the visibility of the class you want to redirect to
i have a project in eclipse for selenium test.
my project contains Test.class and Utility.class
In my Utility.class i have a method like:
public static void UsernameElement(){
try {
driver.findElement(By.xpath(Component._usernamef)).isDisplayed();
System.out.println(" username field is displayed [TEST PASSED]");
} catch (NoSuchElementException e) {
System.out.println("Username field is not displayed [TEST FAILED]");
} finally {
System.out.println("");
}
}
And then in my Test.class, i call the method like this:
Utility.UsernameElement();
Then i got this error:
"The method UsernameElement() is undefined for the type Utility"
i do not know i should put inside bracket in Utility.Username();
Can someone give a clue please. Thank you
Can anyone tell me where I'm going wrong in my program to get this error? I've put an "*" next to the lines which give me this error. Using Eclipse btw. The whole code is linked below. Thanks!!
public static void main(String[] args)
{
openFile();
addRecords();
closeFile();
}
public static void openFile()
{
try
*{
* output = new Formatter("numbers.txt");
*}
*catch
{
System.err.println("Write permission denied. Terminating.");
System.exit(1);
}
*catch
{
System.err.println("Error opening file. Terminating.");
System.exit(1);
}
}
http://pastebin.com/CKPQzCNi
Your catch clause is an exception handler which takes in an argument. The argument type, ExceptionType must be declared and must be the name of a class that inherits from the Throwable class. I see your try-catch block is not specifying an argument to either of the catch clauses.
Refer to the method addRecords() in the code you have referenced on pastebin for an example of correct exception handling.
I'm new to exceptions and am a bit confused at getMessage() and super(customMessage). Please forgive me if it sounds rather ridiculous...
Part of my try catch code is here:
try{
sc.buyASong();
} catch (CardEmptyException e){
System.out.println("Caught error: " +e.getMessage() );
} catch (CardNotActivatedException e){
System.out.println("Caught error: " +e.getMessage() );
}
And I want to print "Caught error: Card not activated"
or "Caught error: No more songs on the card". And my custom exception is like this
class CardNotActivatedException extends Exception{
public CardNotActivatedException(){
super("Card not activated");
}
}
class CardEmptyException extends Exception{
public CardEmptyException(){
super("No more songs on the card");
}
}
But when I run it, it prints
com.example.CardNotActivatedException: Card not activated
and "Caught error: ----" is not printed out as well. Is there something that I am understanding wrongly? Because it looks like I am fundamentally wrong and I'm not quite sure where my misunderstanding is...?
Are you sure you're catching the CardEmptyException ?
Did you forget to save your code?
Anyway, I suggest you change your approach, and use a constructor method receiving a String object. You can keep a default costructor with a default message, but also use somthing like this one:
public MyCustomException(String arg0) {
super(arg0);
}
Then, whenever you call this exception, you can call passa the message you want to.
Suppose, I am testing method which checks whether all links are present on page. Ex :
#Test
public void testLinks(){
driver.findElement(By.linkText("link1"));
driver.findElement(By.linkText("link2"));
driver.findElement(By.linkText("link3"));
driver.findElement(By.linkText("link4"));
driver.findElement(By.linkText("link5"));
}
In above code, suppose all links are present except link3 then test method's execution will stop after throwing error for link3 but still if I want code to be executed for checking link4 and link5, how can I achieve that using Java?
I think you mean throwing exceptions.
You can test links one by one in another method which handle exceptions.
public void testOne(String link) {
try {
driver.findElement(By.linkText(link));
}
catch (Exception e) {
System.out.println(link+" failed to find");
}
}
public void test() {
testOne("link1");
testOne("link2");
testOne("link3");
testOne("link4");
testOne("link5");
}
The testOne() method will catch exceptions and print a failed note, and your test method won't be disrupted.
Try this, you can use TestNg reporter to make your test pass or fail or log4j logs, try this.
sResult = "Pass";
public void testOne(String link) {
try {
driver.findElement(By.linkText(link));
}
catch (Exception e) {
System.out.println(link+" failed to find");
sResult = "Fail";
}
}
public void test() {
testOne("link1");
testOne("link2");
testOne("link3");
testOne("link4");
testOne("link5");
}
if(sResult.equals("Pass")){
System.out.println("Pass");
}else{
System.out.println("Fail");
}