I am using selenium to perform repetitive tasks on a website - a very useful feature for automating web tasks.
I am stuck on how to perform an action only if certain text is present on the page. Is this possible?
I do not want the lack of text to break the test case - only to bypass the action.
I have the Flow Control plugin too, but havent figured out how to make this do any more.
In Java you can make a method that returns true or false whether an element is present or not :
public boolean isElementPresent(By by) {
try {
return driver.findElement(by) != null;
} catch (NoSuchElementException e) {
return false;
}
}
And concerning the text you want to find you can do :
if(isElementPresent(By.xpath("//*[contains(text(),'Some text')]"))){
// Do your tests here
}
Related
I am trying to integrate the Extent Reports with Selenium WebDriver Event listeners so that after every action (like navigateTo, clickon, elementChangeValue, etc) the logs get added to the extent report for every action and exceptions. Any thoughts on how can I achieve this since I think I cant pass the EventTest object as a parameter in extended/implemented WebDriverEventListener's methods.
I don't know if that is possible but you can create you own methods for navigateTo, clickon, elementChangeValue, etc and add the actions like steps in the extent report.
For example:
public void navigateTo(String url) throws Exception {
driver.get(url);
try {
driver.findElement(By.className("some_element_in_page"));
TestListener.getExtentTest().log(Status.INFO, "Login successful");
} catch (Exception e) {
TestListener.getExtentTest().log(Status.FAIL, "Login failed");
}
}
Here is a tutorial that might help you.
I have to make some operations when a Feature file starts or ends.
But I didn't find any way that Selenium can know it.
Meanwhile I use a specific hook tag to catch the beginning and another one to catch the end. But Is there a way to know it in Selenium code?
You can use before and after hook to perform some actions, you can add extra tag to your cucumber scenario and check it by scenario.getSourceTagNames(). see the example below:
#Before
public void setUpScenario(Scenario scenario) {
List<String> tags = scenario.getSourceTagNames();
if (tags.contains(scenario_specific_tag)) {
System.out.println("Before your scenario running ...." );
}
}
#After
public void endUpScenario(Scenario scenario) {
List<String> tags = scenario.getSourceTagNames();
if (tags.contains(scenario_specific_tag)) {
System.out.println("After your scenario ...." );
}
}
I have a Selenium test that is supposed to verify that I'm unable to see a button link unless there is a certain amount of information present on a page such as a pagination link for example. Here is my Selenium assert statement:
def test_bottom_links
load_page('orgs/apache_software')
$driver.find_element(:xpath => "//a[#id='btn_orgs_see_all_projects']").element? == false
end
In my mind this makes sense to me but I receive this error when I run my test:
Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"xpath","selector":"//a[#id='btn_orgs_see_all_projects']"}
The above error is what I want as a passing statement. I don't want Selenium to find this element because it should not be there.
I've also tried this and I get the same error:
$driver.find_element(:xpath => "//a[#id='btn_orgs_see_all_projects']").displayed? == false
I was wondering what the correct syntax should be to make this test pass. I've referred to these linksassertNotSomething and List of Selenium Methods. However, these don't have examples of how they are used so I was wondering how someone would write a test like the above. Thanks for any help offered.
Here's a simple boolean check method that should work.
boolean checkForElementPresence(By locator)
{
try {
driver.findElement(locator);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
You can switch to $driver.find_elements that will return an array without raising an exception. Then you can check for the size of the array to make sure it is empty indicating that the element was not found.
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.
This question already has answers here:
Selenium IDE click() timeout
(2 answers)
Closed 3 years ago.
I have a GridView control with sorting enabled inside an UpdatePanel. I used Selenium IDE to record a test that clicks on the sort link of the table, but when I try to execute the test it get's stuck on the click command. Looking at the log I see:
[info] Executing: |click | link=Name | |
[error] Timed out after 30000ms
I haven't tried it with Selenium-RC yet, I don't know if it will be any different. I don't want Selenium to wait for anything. Any ideas of how to work around it?
Thanks!
when using selenium + Ajax (or the page just get refresh under certain conditions).
I usually use:
selenium.WaitForCondition
or I created the following code recently (the page uses frames).
public bool AccessElementsOnDynamicPage(string frame, Predicate<SeleniumWrapper> condition)
{
DateTime currentTime = DateTime.Now;
DateTime timeOutTime = currentTime.AddMinutes(6);
while (currentTime < timeOutTime)
{
try
{
SelectSubFrame(frame);
if (condition(this))
return true;
}
catch (SeleniumException)
{
//TODO: log exception
}
finally
{
currentTime = DateTime.Now;
}
}
return false;
}
public bool WaitUntilIsElementPresent(string frame, string locator)
{
return AccessElementsOnDynamicPage(frame, delegate(SeleniumWrapper w)
{
return w.IsElementPresent(locator);
});
}
public bool WaitUntilIsTextPresent(string frame, string pattern)
{
return AccessElementsOnDynamicPage(frame, delegate(SeleniumWrapper w)
{
return w.IsTextPresent(pattern);
});
}
Soon you will get to the point you will need selenium RC integrated on your development environment, for this I recommend you to read:
How can I make my Selenium tests less brittle?
It is around waiting but for specific elements that should be (or appear) on the page.