Can I make Behat/ZombieJS/Mink simulate a click on a "non-link" element, to fire JS listeners? - behat

I am using Behat for testing, with the ZombieJS driver, and everything is working very well so far, but there is one thing that bothers me, I can't seem to figure out how to get Behat to simulate a click on a non-link element. I guess this comes from Mink being a "cross-browser" driver, and it doesn't really make sense to click on non-link elements. For my use case though, I need it, because there are several elements on the page that have javascript event listeners attached to them, and I want to test these functionalities.
I tried using
Then I follow "div.sa"
But it fails with the expected error:
01. Link with id|title|alt|text "div.sa" not found.
Since it's not a link element. There is a similar step in Behat:
I press "button.class"
But this one is only for buttons.
Is there a way I could get Behat click on elements that are not tags?

With some error handling:
/** Click on the element with the provided xpath query
*
* #When /^(?:|I )click on the element "([^"]*)"$/
*/
public function iClickOnTheElement($locator)
{
$session = $this->getSession(); // get the mink session
$element = $session->getPage()->find('css', $locator); // runs the actual query and returns the element
// errors must not pass silently
if (null === $element) {
throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $locator));
}
// ok, let's click on it
$element->click();
}

Yes I believe so, but you'll need to write a custom step, something along these lines (if you're using a subclass of MinkContext.
/**
* #When /^I click the something$/
*/
public function iClickTheSomething()
{
$something = $this->getSession()
->getPage()
->find("css", "div.sa")
->click();
}

This is my adaptation of the answer by Comstar. The same functionality is achieved, however, it seems simpler to me to do it this way.
/**
* #Then /^I click on "([^"]*)"$/
*/
public function iClickOn($arg1)
{
$page = $this->getSession()->getPage();
$findName = $page->find("css", $arg1);
if (!$findName){
throw new Exception($arg1." could not be found");
}
else {
$findName->click();
}
}

When I execute a click event, so that the current URL be equal to javascript:
$this->iClickOnTheElementWithXPath(‘//div[#class="'.$arg2.'"]/div/div[#class="edit"]/a[text()="'.$arg1.'"]‘);
In the page html:
<a onclick="editForm('Avatar')" href="javascript:;">Modifier</a>

Related

selenium - how to find xpath

can any one help me for finding the correct xpath for the given link Logout
What you could do is to locate all links in your page, and then file the one corresponding to what you're searching for.
Here is the code converted to java
public static IWebElement GetLinkContainingText(string textToBeContained) {
// Here Driver is my ChromeDriver instance. You can replace a, by whatever tag your href is in.
ArrayList<WebElement> allTags = Driver.FindElements(By.Xpath("//a"));
for (WebElement v : allTags) {
if (v.GetAttribute(href).contains(textToBeContained)) {
return v;
}
}
return null;
}
calling the method would result for you in that.
WebElement elementYouSeachFor = GetLinkContainingText("http://ec2-34-210-163-161.us-west-2.compute.amazonaws.com:8094/login/index/logout");
Most likely
WebElement elementYouSeachFor = GetLinkContainingText("/logout");
would work too since there's probably not many links with logout on your page.
Hope this helps.
driver.findElement(By.xpath("//a[text()='Logout']"))
Is one way.
driver.findElement(By.linkText("Logout"));
Both could be problematic if you have more than 1 logout link on the page.
More resources on selecting elements: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html

click only visible element selenium webdriverjs

I have multiple div like below
<div class="one">send Message</div>
<div class="one">send Message</div>
<div class="one">send Message</div>
I have a web page where there is send Message buttons like above, in which only one button is visible at a time.Other two buttons are hidden via some javascript codes.So for example if 2nd button is visible , I should be able to click only that element.But in my selenium code , its trying to click first hidden div and its failing
driver.findElements(by.className(".one")).then((els) => {
var element = els[index];
element.click();
});
So basically I wanna convert below javascript code to Selenium nodejs code,If some one guide me that will be helpful
var all = document.getElementsByTagName("*");
for (var i = 0, max = all.length; i < max; i++) {
if (isHidden(all[i]))
// hidden
else
// visible
}
function isHidden(el) {
var style = window.getComputedStyle(el);
return ((style.display === 'none') || (style.visibility === 'hidden'))
}
Do you want to click the button ( basically a div as far as code is concerned ) which is visible ?
If that is your main agenda, then the code you've written will fail to find desired element. As you are selecting the element by it's classname not its visibility.
Your code will find all the matched class element. As it's a basic element selector and all your buttons have the same class, so they are basically rendered on the page.
Approach 1
driver.findElements(by.className(".one")).then((els) => {
for(var key in els){
var element = els[key];
if(element.isDisplayed()){ //if visible element
element.click();
}
}
});
The Key here is to check if the element you are trying to click is visible on the page.
Approach 2
By giving a unique class to the target button. Some class for eg 'clickable' or 'active'. So it will be a more optimized solution to select the target element using the Css selector. The Key here is to give uniqueness to your target element to be more identifiable.
Usually many Java Scripts are run in the node Js without the convert.
Have you try it in the node Js without converting ???
** Remember to import selenium

Click Next link until pagination Next action is disabled in selenium webdriver

I want to verify elements in a row inside a table in by navigating each page through pagination.I am able to navigate to each page and assert element but the probelm is at last page the loop still continues even though the Next link is grayed out.
When Next link is disabled
<span>
<a class="next_btn inactive">NEXT ></a>
</span>
When Next link is enabled
<span>
<a class="next_btn" href="home.do?action=Next&start=10&end=20&sort=&
type=00&status=&fromDate=04/02/2017&toDate=05/02/2017&school=&
district=0009">NEXT ></a>
</span>
Actual Code
public void submissionType() throws Exception {
driver.findElement(By.linkText("NEXT >"));
while(true) {
processPage();
if (pagination.isDisplayed() && pagination.isEnabled()){
pagination.click();
Thread.sleep(100L);
} else
break;
}
}
private void processPage() {
String statusColumn="//td[#class='gridDetail'][2]";
List<WebElement> list = table.findElements(By.xpath(statusColumn));
for (WebElement checkbox : list) {
String columnName=checkbox.getText();
Asserts.assertThat(columnName,"File");
}
}
Instead of identifying the element with By.linkText("NEXT >"), try identifying it with By.cssSelector("a.next_btn").
When you use this approach, then when the object becomes disabled, its class name would change, and hence your object would no longer get identified and your loop would break.
Edit: Add try block and Catch NoSuchElement exception to avoid exceptions.
I know you already accepted an answer but one of the statements is not correct. The locator, By.cssSelector("a.next_btn"), will find both the enabled and disabled button so it will not cause the loop to break.
Looking at your code, I would offer a few suggestions/corrections.
.isEnabled() only really works on INPUT tags so testing for that doesn't really accomplish anything here.
Using Thread.sleep() is not a good practice. You can google some explanations but basically the problem is that it's an inflexible wait. If the element you are waiting for becomes available in 15ms, you are still going to wait 10s or whatever your sleep is set to. Using an explicit wait (WebDriverWait) is a best practice.
I would clean up your functions and write them like
public void submissionType()
{
WebDriverWait wait = new WebDriverWait(driver, 10);
By nextButtonLocator = By.cssSelector("a.next_btn:not(.inactive)");
List<WebElement> nextButton = driver.findElements(nextButtonLocator);
while (!nextButton.isEmpty())
{
processPage();
nextButton.get(0).click();
wait.until(ExpectedConditions.stalenessOf(nextButton.get(0))); // wait until the page transitions
nextButton = driver.findElements(nextButtonLocator);
}
}
private void processPage()
{
for (WebElement checkbox : table.findElements(By.xpath("//td[#class='gridDetail'][2]")))
{
Asserts.assertThat(checkbox.getText(), "File");
}
}

Can't click on submit button. Behat/Mink

I have a form with submit button
<input id="myid" type="submit" value="My button"></input>
I tried a lot different ways to click on it, but it doesn't work. Why? I use Selenium Driver with PhantomJS browser.
What I tried:
$page = $this->getSession()->getPage();
$page->find('xpath', '//*[#id="myid"]')->click();
$page->find('xpath', '//*[#id="myid"]')->doubleClick();
$page->find('xpath', '//*[#id="myid"]')->press();
$page->find('css', '#myid')->click();
$page->find('css', '#myid')->doubleClick();
$page->find('css', '#myid')->press();
$this->getMinkContext()->pressButton('myid');
We use a function like this:
/**
* #Then /^(?:|I )click (?:on |)(?:|the )"([^"]*)"(?:|.*)$/
*/
public
function iClickOn($arg1)
{
$findName = $this->getSession()->getPage()->find("css", $arg1);
if (!$findName) {
throw new Exception($arg1 . " could not be found");
} else {
$findName->click();
}
}
Usage 1:
Scenario: Test Click
Given I go to "<url>"
And I click the "#myId" button - to reveal a hidden element
Then I should see an "<url2>" element
Usage 2:
Scenario: Test Click
Given I go to "<url>"
And I click the "#myId" link
Then I should be on "<url2>"
If this doesn't work for you, then it may be because there is an element in the way? Is the button that you want to click on genuinely there?
If it isn't, then perhaps this will help you too:
/**
* #When I scroll :elementId into view
*/
public function scrollIntoView($elementId) {
$function = <<<JS
(function(){
var elem = document.getElementById("$elementId");
elem.scrollIntoView(false);
})()
JS;
try {
$this->getSession()->executeScript($function);
}
catch(Exception $e) {
throw new \Exception("ScrollIntoView failed");
}
}
Personally, I haven't used the final function, however the same function helped a person in one of the posts I was viewing before this.

Selenium Xpath Not Matching Items

I am trying to use Selenium's Xpath ability to be able to find an set of elements. I have used FirePath on FireFox to create and test the Xpath that I have come up with and that is working just fine but when I use the Xpath in my c# test with Selenium nothing is returned.
var MiElements = this._driver.FindElements(By.XPath("//div[#class='context-menu-item' and descendant::div[text()='Action Selected Jobs']]"));
and the Html looks like this:-
Can Anyone please point me right as everything that I have read the web says to me that this Xpath is correct.
Thanking you all in-advance.
Please post the actual HTML, so we can simply "drop it in" into a HTML file and try it ourselves but I noticed that there is a trailing space at the end of the class name:
<div title="Actions Selected Jobs." class="context-menu-item " .....
So force XPath to strip the trailing spaces first:
var MiElements = this._driver.FindElements(By.XPath("//div[normalize-space(#class)='context-menu-item' and descendant::div[text()='Action Selected Jobs']]"));
Perhaps you don't take into consideration the time that the elements need to load and you look for them when they aren't yet "searchable". UPDATE I skipped examples regarding this issue. See Slanec's comment.
Anyway, Selenium recommends to avoid searching by xpath whenever it is possible, because of being slower and more "fragile".
You could find your element like this:
//see the method code below
WebElement div = findDivByTitle("Action Selected Jobs");
//example of searching for one (first found) element
if (div != null) {
WebElement myElement = div.findElement(By.className("context-menu-item"));
}
......
//example of searching for all the elements
if (div != null) {
WebElement myElement = div.findElements(By.className("context-menu-item-inner"));
}
//try to wrap the code above in convenient method/s with expressive names
//and separate it from test code
......
WebElement findDivByTitle(final String divTitle) {
List<WebElement> foundDivs = this._driver.findElements(By.tagName("div"));
for (WebElement div : foundDivs) {
if (element.getAttribute("title").equals(divTitle)) {
return element;
}
}
return null;
}
This is approximate code (based on your explanation), you should adapt it better to your purposes. Again, remember to take the load time into account and to separate your utility code from the test code.
Hope it helps.