'Assert cannot be resolved' is displayed when i tries to insert an Assert command
String menu1 = driver.findElement(By.xpath("value")).getText();
System.out.println(menu1);
if(Assert.assertEquals(menu1,"About Us"))
{
}
The error says it all :
'Assert cannot be resolved'
The reason for this error and the solution can be either of the following:
import org.testng.Assert; is missing.
TestNG jars were not downloaded properly. You may need to download the TestNG jars and reconfigure.
It is worth mentioning that Assert.assertEquals() method returns void. So the compiler should be complaining about the following line of code:
if(Assert.assertEquals(menu1, "About Us"))
If you have a requirement to catch the Pass or Fail status, wrap up the assertion in a try-catch block with Throwable t as follows:
String menu1 = driver.findElement(By.xpath("value")).getText();
System.out.println(menu1);
try {
Assert.assertEquals(menu1, "About Us")
System.out.println("Assertion Successful");
} catch (Throwable t) {
System.out.println("Exception raised in Assertion");
}
Output on success will be:
Assertion Successful
Output on failure will be:
Exception raised in Assertion
please add the below import statement to your code. it may resolve your issue.
import org.testng.Assert;
assertEquals doesn't return any value.
You can't put inside a if clause. please remove if statement.
just add only the assert statement.
Assert.assertEquals(menu1,"About Us");
No need to use if with Assert, try it seperately. Also check whether testng is there are not,
use
import org.testng.*;
or
import org.testng.Assert;
Like this code:
String menu1 = driver.findElement(By.xpath("value")).getText();
System.out.println(menu1);
Assert.assertEquals(menu1,"About Us");
Just Right Click on the folder which that we need to implement assertion just right click >>> Build Path >>> Add Libraries >>> TestNG
Related
I need to get output result (FAIL or SUCCESS) into log4j output.
Assert.assertTrue(availresponse);
Is there any way to add TestNG console output into log4j.log file?
I found a simple, easy and perfect way to log TestNG output into log4j.log file. But this can't be log Final Report Details into log4j (PASS or FAIL).
implements ITestListener
Then add unimplemented methods
public void onTestSuccess(ITestResult result) {
logger.info(result);
}
public void onTestFailure(ITestResult result) {
logger.info(result);
}
You can add log4j logger in the methods you need. Enjoy!
If you need that, you could use if .. else construct to do the logging.
if(null != availresponse) {
//log success message
} else {
//log failure message
}
Another way is to write a custom class implementing TestListenerAdapter. You can see full example code here.
Wrap the assert with a logger e.g.
log.info(Assert.assertNotNull(availresponse));
However this is a bit limited in terms of information. What I usually do is:
if(availresponse==null)
{
log.fatal("availresponse was null");
}
Assert.assertNotNull(availresponse);
I know it's a bit of a hassle, testing for the condition twice but it does allow you to tweak the output level of the logger and add any other information you think would be useful e.g. the variables which were used to determine the contents of availresponse.
Alternatively you could try getting the console output to appear in your log file in which case you'll need to add something like:
log4j.appender.stdout.Target=System.out
to your log4j properties file.
You can find test-output folder in your project PATH. index.html will show the Test output of Success or Fail.
If the testMethod() is Success, there will no any output result shows in index.html file. You can add Success message by using Reporter.log()
If testmethod() fail, automatically it will prints output in file.
NOTE : java.lang.AssertionError is an error that thrown to indicate that an assertion has failed.
If anyone know how TestNG inserting that error to the report, comment below.
Finally found the easiest way to log the Assert error in log4j
In catch block it should be Throwable, if it is Exception that will not work.
try {
Assert.assertTrue(hotel.getAmenitiesList().size() < 0, "Hotel Amenities Available!");
} catch (Throwable e) {
e.printStackTrace();
logger.error("FAILED: testRoomAmenities ", e);
Assert.fail();
When this implement that will not invoke as FAIL. It will show that scenario is PASSED. Therefore you should use Assert.fail() to make that is FAILED
i have been searching around for a few days for the answer to this and i just can't seem to get it to work. I have seen exact examples where it is working for them and I try exactly what they do and it is just not working for me.
Basically what i am trying to do is open a local access DB. I have tried numerous methods and this Jackcess seems by far the best library to use, so i am trying to get it to work with that. I have read their cookbook and gone through all of that, and still no luck so i am coming to you guys in the hope of finding a good solution (i have not posted this question anywhere yet). Here is my code (the relevant part)
The only syntax error i am getting is "DatabaseBuilder.Open" and the error is it cannot find the method, even though i have the libraries included for IO
import com.healthmarketscience.jackcess.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Database db = DatabaseBuilder.open(new File("my.mdb"));
try {
Table table = db.getTable("Teams");
} catch (IOException ex) {
Logger.getLogger(Teams.class.getName()).log(Level.SEVERE, null, ex);
}
}
Any help would be greatly appreciated!
The program fails to debug once I have click this buttone the only actual message i can fine is
"Cannot find symbol
Symbol : Method Open(file)
Location : variable.DatabaseBuilder of type Object"
To use Jackcess you must have at least the following items in the build path for your Java project (or on your CLASSPATH):
the Jackcess JAR file itself, and
the JAR files for its two mandatory compile-time dependencies from Apache: commons-lang and commons-logging.
In the Eclipse IDE that will look something like this:
I am leaning selenium. I have trouble to run a sample script. Please help me to figure out what I did wrong. Thanks!
My installations:
JDK1.7.0._02
selenium-server-standalone-2.31.0.jar
Eclipse IDE 3.7.0
Selenium IDE 1.9.0 (Firefox plugin)
Eclipse indicates the following error message when I click on the package section in the code
1.the declared the package org.openqa.selenium.example; doesn't match expected package Seletest1
2. syntax error on token package, imported expected
Eclipse also suggested move Test1.java to package 'org.openqa.selenium.example
Please suggest the right action for me to take, should I imported org.openqa.selenium.example into the build path of my project or should I move Test1.java into the package?
where Can I find out the location of the package-org.openqa.selenium.example?
Here is my code copied from Google code get started with Selenium
my project structure SeleniumTest1>Src>SeleTet1
package SeleTest1;
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Test1
{
public static void main(String[] args)
{
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
}
}
The error message showed when I execute the code
When I run the code in Eclipse, I received the following error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)
You have a duplicate package declaration on the top of your code. I would remove the second one (org.openqa.selenium.example) since your code is probably in the SeleTest1 folder.
Your package declaration is not required to match the one of the framework you are using.
When you export the selenmium IDE recorded test case into webdriver format, by default the package statement will get added as
package org.openqa.selenium.example;
We need to modify it as per our package name created in Eclipse.
So, in your case, you can remove the below duplicate line.
package org.openqa.selenium.example;
You will not get the 2nd error also once you have done this modification.
Dear community members,
I have a small problem with the following code. I think it should open the explorer in the C:\Users\Me\AppData\Local\Temp\ directory. However that does not work, actually nothing happens. No errors.
I have used the following code:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
Desktop.getDesktop().open(File.createTempFile("abcd", ".temp").getParentFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
If I replace it with a normal file, like new File("C:\"), then it does work. Can someone explain to me why it does not work?
PS: guys I forgot to tell you I also tried it with some characters like "abcd", it still gives nothing and shows nothing!
Just use new File(System.getProperty("java.io.tmpdir")): that's the temp directory. No need for dirty tricks with the parent of a useless temporary file...
Looking at the Javadoc for the File class:
Parameters:
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
So it appears that "" isn't a valid argument for the file prefix.
According to the docs for File.createTempFile(), if the prefix (first argument) contains fewer than three characters, an IllegalArgumentException will be thrown. You should see it in your console output.
Im am currently developing an automated "test" class (running several individual tests on other classes in the same package). The aim of the test file is to show whether each test either passed or failed. Some of the files being tested are not written correctly creating an ArrayOutOfBoundsException when running the test, that produces my test file to crash and not carry on until the end performing other tests. I am not in the position to modify code to fix the errors on the project being tested.
-> how to stop an exception from halting program execution in Java without creating any new classes
Thank for all your help, advice and sharing.
Best way to stop it happening: fix the code to perform appropriate checking.
If you can't fix the code which is actually failing, you could catch the exception explicitly at an "outer" level, log a warning and continue with the next file:
try
{
operationWhichMightThrow();
}
catch (ArrayIndexOutOfBoundsException e)
{
log.warning("Failed file " + filename, e);
// Do whatever you need to continue to the next file.
}
Catch the exception and log it as a test failure.