I'm using maven and selenium for testing in java. I would like to know if there is a way to call a function every time a test fails. I already have a function that takes an screenshot of the browser. I would like to use it every time selenium throws NoSuchElNoSuchElementExeption for example.
Is there an "easy" way to implement this?
Thank you!
You can create a method to find the element and implement a try-catch block inside that to catch the NoSuchElementException. Call your function to take the screenshot inside that catch block.
e.g.
public WebElement findElement(By locator) {
try {
WebElement element = driver.findElement(locator);
return element;
} catch (NoSuchElementException nse) {
// call the function to take a screenshot
}
}
I eventually found this solution to the problem:
https://darrellgrainger.blogspot.com/2011/02/generating-screen-capture-on-exception.html?m=1
Consists on using WebDriverEventListener.
Related
clear method clears the value but retains and when send keys new value is passed the text box shows previous value + new value in selenium Appium. Kindly suggest.
Version:java-client-4.1.2
Appium Server:v1.7.2
Selenium: 3.0.1
I tried this but it did not work out.
public void clearTextBox(WebElement element,String text) throws Exception
{
element.click();
Thread.sleep(3000);
element.clear();
element.sendKeys(text);
}
Getting this
The reason your code is failing (and you should have included your code in the original question) is because you are doing it through TWO separate calls to the page object element.
Each time you call the page object, it looks-up the element freshly, so the first time you call it and issue a .clear() and then you are calling it again after an unnecesary sleep with the .sendkeys() method. The click is also unnecessary.
You should write a public method in your page object model to perform the sendkeys() for you which does the clear() and then the sendkeys(), i.e.:
public void setMsisdnValue(String text) {
Msisdn.clear();
Msisdn.sendKeys(text);
}
Personally, I use a set of helper methods so that I can error-trap and log things like sendkeys, but I would still call it from within the page object model itself. That helper would do the same two steps, but also do it inside a try/catch and report any errors, so it would be simplified to:
public void setMsisdnValue(String text) {
helper.sendKeys(Msisdn, text);
}
Hope this helps.
I am new to Selenium.
My issue is that I'm trying to click an element but Selenium is throwing a timeout exception, even if I increase the timeout value.
Do I need to use xpath instead of id?
The HTML Code is:
My code looks like this
void searchquotation() throws TimeoutException {
try {
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("SearchButton")));
element.click();
}
catch(TimeoutException e) {
System.out.println("Timeout occured");
}
Am I doing anything wrong?
The input type here is submit (by looking at your HTML code) so I would strongly advise to try the submit() function of Selenium.
Instead of By.name, you should use By.id instead. Therefore, use either of these:
By.Id("SearchButton")
By.CssSelector("input#SearchButton")
By.Xpath("//input[#id='SearchButton']")
Note: syntax could be wrong, please adjust depending on your programming language
try below code, even timeout exception occurs, it will try 4 time to click on it. assuming locator is correct By.name("SearchButton")
public void searchquotation()
int count=0;
while(count<4)
{
try {
WebElement x = driver.findElement(By.name("SearchButton")));
WebDriverWait element=new WebDriverWait(driver,15);
element.until (ExpectedConditions.presenceOfElementLocated(By.name("SearchButton")));
x.click();
count=count+4;
}
catch(TimeoutException e) {
count=count+1;
System.out.println("Timeout occured");
continue;
}
}
I am trying to locate element which may have different ID at times.
Here is the example :
id = 'greenbay_packers"
id = "Sf_49ers"
Now, is there a way do to some kind of OR operation in find_element method? so that I can use same element locator for test steps?
Also if this is not possible, is there a way to write fail safe routine that try to locate using find_element(:id,'greenbay_packaers") but if fails try find_element(:id,'sf_49ers"). And only fail test if above 2 are not found.
thanks
It is possible with or
//*[(#id='test1') or (#id='test2')]
For second part,
I would suggest you to try try..catch..finally since you have only two conditions to match
try
{
Driver.FindElement(By.Id("ID1"));
}
catch (NoSuchElementException ex)
{
Driver.FindElement(By.Id("ID1"));
}
finally
{
Console.WriteLine("Failed");
}
Written in C#
Is there a way for Selenium/Cucumber to throw a more descriptive error when an element is not found? Currently it only throws:
The element could not be found (Selenium::WebDriver::Error::NoSuchElementError)
I'd like to also include the locator.
It depends how your test fixture code is designed and structured. You can wrap FindElement code inside try/catch block. Something like:
Public void TestMethod(string elementId)
{
try
{
driver.FindElement(By.CssSelector(elementId));
}
catch(NoSuchElementException exception)
{
Assert.Fail("Can't find the element in the page. The element is: " +elementId)
}
}
I am using below code to check for element on my web page
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
catch (Exception e)
{
return false;
}
}
I need to check in my program if a particular region appears in result as below
isElementPresent(By.xpath(".//*[#id='header']")));
If this is present this function completes quickly but if above is not present then it run for very long.
Could some one please help me in resolving this issue so that this check can be performed quickly?
Here you are missing somethings that is why it is waiting If there is not element. findElement will wait for an element implicitly specified time. so need to set that time to zero in that method.
isElementPresent(WebDriver driver, By by) {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
} finally {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
There are 4 important things going on here. In order:
Setting implicity_wait to 0 so that WebDriver does not implicitly wait.
Returning True when the element is found.
Catching the NoSuchElementException and returning False when we discover that the element is not present instead of stopping the test with an exception.
Setting implicitly_wait back to 30 after the action is complete so that WebDriver will implicitly wait in future.
Apparently, it's long to send the exception because your DOM is big and the xpath isn't the fastest way to get an element. But if you want to use xpath, try to put the best path and avoid that kind of function where is substring checking.
Your actual xpath : .//*[#id='header'] takes so long because you check all tags of your DOM. So if put the tag that what you're looking for, example : you want to catch an input. your xpath should start like that //input[#id='1234'] and it will be shorter than looking all tags.