Verify element is not displayed - java

I want to check that an element is not displayed. I have written the following code :
public boolean verifyelementNotDisplayed() {
try
{
if(element("element").isDisplayed())
return false;
else
{
logMessage("element not displayed");
return true;
}
}
catch(Exception e)
{
return false;
}
}
But my test fails.

Try this snippet:
public boolean verifyelementNotDisplayed() {
try{
return(!element("element").isDisplayed());
catch(Exception e){
return false;
}
}
And have a look at the is displayed method:
How does Selenium WebDriver's isDisplayed() method work
or:
Where is the implementation for the WebElement.isDisplayed() method in Selenium?

The following code worked
public boolean verifyNoelement() {
try
{
if(element("element").isDisplayed())
{
return false;
}
return false;
}
catch(Exception e)
{
logMessage("No element displayed");
return true;
}
}

Related

Selenium click until the attirbute show

How can I create click method which can be click in the button until the attribute shows? Something like loop until but with timer (if it's not found attribute after 10 seconds, display an error). I have created something like that, but code give me NullPointerException when not found attribute:
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
WebElement button = driver.findElement(By.xpath("xpath"));
String attribute = button.getAttribute("disabled");
button.click();
if(attribute .equals("true"))
return true;
else
return false;
}
});
I got similar problem and my solution was to write a function that tries to click on the object and when it is not present it waits a second and tries once again. After 10 times of trying it returns null.
It works beautifully for me. Goes like this:
WebElement tryfind(WebDriver browser, By id)
{
WebElement ttwel=null;
for (int i=0;i<10;i++)
{
try
{
ttwel=browser.findElement(id);
break;
}
catch (NoSuchElementException ex)
{
sleep(1000);
}
}
return ttwel;
}
void sleep(int mills)
{
try
{
Thread.sleep(mills);
}
catch (InterruptedException ex)
{
}
}
You might add waiting for disabled attribute to this method, something like:
for (int i=0;i<10;i++)
{
try
{
ttwel=browser.findElement(id);
String attribute = button.getAttribute("disabled");
if (attribute==null) sleep(1000);
else break;
}
catch (NoSuchElementException ex)
{
sleep(1000);
}
}
Why don't you add a try catch block like this below to catch the NullPointerException and return false in that case
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
WebElement button = driver.findElement(By.xpath("xpath"));
try {
String attribute = button.getAttribute("disabled");
button.click();
if (attribute.equals("true")) {
return true;
}
} catch (NullPointerException e) {
return false;
}
return false;
}
});

How to check if string-number with decimals is double or integer?

I'm reading values from a .csv file and there are numbers in which are stored in the format #.##, i.e. the number 4 is stored as 4.00.
Now I need to check if the nuber is an integer or a double value. Depending on the type I need to format the string in the right way.
I made two checks to validate the value but it never passes the integer-check.
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException | NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
public static boolean isDecimal(String s) {
try {
Double.parseDouble(s);
} catch (NumberFormatException | NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
Is there a easy way how I can check this?
I would do something like this:
public boolean isInteger(String s) {
boolean result;
try {
double d = Double.parseDouble(s);
if ((d == Math.floor(d)) && !Double.isInfinite(d)) {
result = true;
}
} catch (NumberFormatException e) {
result = false;
}
return result;
}
public boolean isDecimal(String s) {
boolean result;
try {
double d = Double.parseDouble(s);
if (d != Math.floor(d)) {
result = true;
}
} catch (NumberFormatException e) {
result = false;
}
return result;
}
Try this:
private static boolean isInteger(String s) {
return Double.parseDouble(s) % 1 == 0;
}

Selenium WebDriver how to close browser confirmation popup with OK and Cancel button

I am writing tests for a web App using selenium webDriver and came across a scenario where when I try to delete a link the browser I get a popup saying:
Delete : "dummyname" Are you sure?
The browser page is asking you to confirm that you want to delete data, with 2 buttons: OK and cancel.
How do I click on those buttons?
For ok
driver.switchTo().alert().accept();
For cancel
driver.switchTo().alert().dismiss();
Hope it will help you :)
You should throw something like this in your BasePage.java and call acceptAlert() or dismissAlert(), but the magic lies in the code as Shubham Jain described above.
protected void acceptAlert() {
waitFor(new BooleanCondition() {
public Boolean apply(WebDriver webDriver) {
try {
webDriver.switchTo().alert().accept();
return true;
} catch (WebDriverException ex) {
return false;
}
}
public String describeFailure() {
return COULD_NOT_LOCATE_OR_ACCEPT_ALERT_BOX;
}
});
}
protected void dismissAlert() {
waitFor(new BooleanCondition() {
public Boolean apply(WebDriver webDriver) {
try {
webDriver.switchTo().alert().dismiss();
return true;
} catch (WebDriverException ex) {
return false;
}
}
public String describeFailure() {
return COULD_NOT_LOCATE_OR_ACCEPT_ALERT_BOX;
}
});
}

Validating absence of an element

I have a test case:
Assert.assertTrue(test .verifyNoDrillDisplayForCourses());
and a boolean method verifyNoDrillDisplayForCourses which verifies element("xyz") is not displayed,
try{
if(element("xyz"). isDisplayed())
return false;
else return true;
}
catch (Exception e)
{
return false;
}
}
But the assertion fails as java .lang .AssertionError:expected [true] but found [false]. I am unable to figure out why?
The isDisplayed() method will throw an StaleElementReferenceException, if the given element is not in the DOM anymore. So you have to change the catch statement to return true;.
If you're testing for the presence of an element, if it's not found an exception will be thrown. So if you find it you're returning false, if you can't find it you're also returning false.
When testing for non-presence of an element you should have the catch block return true!
try{
if(element("xyz").isDisplayed()) {
return false;
} else return true;
}
catch (Exception e)
{
return false;
}
}
I believe your if statement is missing correct formatting from what you copied over.
I've amended it above, but in case try it like this:
if(element("xyz").isDisplayed()) {
return false;
} else return true;
Following code helped:
public boolean verifyNoelement()
{
try
{
if(element("element").isDisplayed())
{
return false;
}
return false;
}
catch(Exception e)
{
logMessage("No element displayed");
return true;
}
}

Browser Java Plugin Detection

What is the preferred method to determine if the Sun Java Plugin is installed in the browser?
java deployment toolkit
script src="http://java.com/js/deployJava.js"
if (deployJava.versionCheck('1.6'))
{
alert("1.6 installed")
}
You may also consider PluginDetect script.
This isn't an answer for your exact question but is offered as a solution for determining the browser itself. Don't be too harsh, this is really old code that I wrote some time ago.
import java.applet.*;
public class BrowserDetector extends Applet {
public void init() {
if (isNetscape()) {
System.out.println("This browser is a Netscape Browser.");
}
if (isMicrosoft()) {
System.out.println("This browser is a Microsoft Browser.");
}
System.out.println("VM Type: " + getVMType());
}
public static boolean isNetscape() {
try {
Class.forName("netscape.applet.MozillaAppletContext");
} catch (ClassNotFoundException e) {
System.out.println("This browser is not a Netscape Browser.");
return false;
}
return true;
}
public static boolean isMicrosoft() {
try {
Class.forName("com.ms.applet.GenericAppletContext");
} catch (ClassNotFoundException e) {
System.out.println("This browser is not a Microsoft Browser.");
return false;
}
return true;
}
public String getVMType() {
String theBrowser = "No VM";
String appletContext = getAppletContext().toString();
if (appletContext.startsWith("sun.applet.AppletViewer"))
theBrowser = "APPLETVIEWER";
else if (appletContext.startsWith("netscape.applet."))
theBrowser = "NETSCAPE";
else if (appletContext.startsWith("com.ms.applet."))
theBrowser = "MICROSOFT";
else if (appletContext.startsWith("sunw.hotjava.tags.TagAppletPanel"))
theBrowser = "HOTJAVA";
else if (appletContext.startsWith( "sun.plugin.navig.win32.AppletPlugin"))
theBrowser = "NETSCAPEPLUGIN";
else if (appletContext.startsWith( "sun.plugin.ocx.ActiveXApplet"))
theBrowser = "MICROSOFTPLUGIN";
else if (appletContext.startsWith( "sun.plugin.viewer.context.IExplorerAppletContext"))
theBrowser = "MICROSOFTPLUGINJRE1.4";
return theBrowser;
}
}

Categories