How to verify contents are greyed out - ruby

The application for which I'm writing script (using selenium-webdrive with Ruby) is built in AJAX. In that, I want to check whether contents are greyed out-
I clicked on button
Then div is loaded but I can see contents are greyed out & following div in firebug -
<div id="bottomHalf" class="disabled" style="background-color: transparent;">
To check whether it's greyed out, I'm confused how to write code. Based on my knwoledge, I could only find element -
driver.find_element(:id, "bottomHalf")
But I don't know how to write code for checking class="disabled" so that I can verify that contents are really greyed out.

You can use CSS selectors instead of bare ID selection.
driver.find_element(:css, '#bottomHalf.disabled')
More info about available selection options is available here

You are in right direction mate...After finding an element you can retrieve the Attributes of that Element. In Ruby use the following method.
# get an attribute
class_name = element.attribute("class")
Here is an example in C# for retrieving attribute values after finding an element.
driver.Navigate().GoToUrl("http://google.com");
IWebElement Element = driver.FindElement(By.Id("gbqfq"));
String Class_name = Element.GetAttribute("Class");
Check this URL for more Ruby Selenium specific Bindings
http://code.google.com/p/selenium/wiki/RubyBindings

Related

Handling Element is not clickable at point (x, y). Other element would receive the click: With Capybara, ruby, Webdriver

I am writing a UI test that goes to a list of articles and selects the load_more button until this is no longer available (because all articles have loaded). The problem is the button has been hidden at this point rather than removed and is returning this error
Selenium::WebDriver::Error::UnknownError: unknown error: Element <div
class="">...</div> is not clickable at point (x, y). Other element
would receive the click: <div class="" id="" data-module-name="">...
</div>
(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.36.540469
ruby-2.5.1/gems/selenium-webdriver
3.11.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok'
I have tried a few loop types but for this, if we assume I'm using
while world.page.has_load_more?
world.page.load_more.click
break if world.page.has_load_more? == false
end
The question is how do I tell Selenium this is ok and not to error, so I can continue with the next step in my test case(cucumber). I realise that the choice of loop type may also be incorrect, so feel free to suggest changes there as well.
Firstly, as Rajagopalan has pointed out, the error that you're getting is not that your element is not visible (as in hidden by CSS) but that it is overlapped by another element. You can call save_and_open_screenshot to see what element is overlapping the button (possibly a fixed footer, etc?). Make sure you've set the window size large enough so you don't have elements unintentionally overlapping and make sure you're running the latest version of Capybara since it attempts to deal with overlapped elements in selenium by scrolling them into view if possible.
Secondly, the loop in your question doesn't make a lot of sense having both the while and break since they check the same thing. Also, assuming the "load more" button is loading and appending to the page via AJAX, you probably need to wait for the new elements to load before checking if the button exists (click does not wait for actions to complete since it has no idea what actions could/would be triggered). That would then become a structure something like
while page.has_button?('Load More')
# Get the current number of visible articles
article_count = page.all('div.article').count # Use whatever selector would match an article in the list
# click the button
page.click_button('Load More')
# ensure more articles have loaded
expect(page).to have_css('div.article', minimum: article_count + 1)
end
Note: You'll probably want to change that to use whatever page object methods you are using (has_load_more?, etc) but the general logic should be correct

How to get the text of hidden check boxes inside a scroll bar using Selenium WebDriver?

I'm trying to automate Filter By Brand scenario in BigBasket and now stuck up in a situation where my code could not print the brand names that are hidden inside a scroll bar.
Steps to follow
Go to www.bigbasket.com
Click Skip & Explore button
Search Apple and view the list of brands on the left side
#FindAll({#FindBy(xpath="//*#id='filter_brands_list']/div/div1/li/label")})
List chkBrands;
The above lines of code identifies all the brand names but when I print them using the below code I can see only the brand names that are visible
for(WebElement eachElement:chkBrands){
System.out.println("No. of brands is "+chkBrands.size());
System.out.println(eachElement.getText());
}
Could you please let me know the solution? I apologize that I could not think of a solution as I'm an amateur in Selenium.
WebElement.getText() will return the text of the element only if it is displayed on the screen.
Meaning if the isDisplayed method returns false then getText()method will return empty.
Explanation with Examples Here
As defined in WebDriver spec, Selenium WebDriver will only interact
with visible elements, therefore the text of an invisible element will
always be returned as an empty string.
However, in some cases, one may find it useful to get the hidden text,
which can be retrieved from element's textContent, innerText or
innerHTML attribute, by calling element.attribute('attributeName') or
injecting JavaScript like return arguments[0].attributeName.
So you can get the text by textContent attribute
eachElement.getAttribute("textContent")
Try this one:
JavascriptExecutor je = (JavascriptExecutor)driver;
WebElement element = driver.findElement(By.xpath("Your_xpath"));
String text_value = ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML;",element);

How to set a span value with capybara?

Does anyone know how to set a value to span tag using capybara?
I tried using element.set or element.send_keys, they only selected the targeted element without modifing the previous value.
<div data-offset-key="bbpvo-0-0" class="_1mf _1mj"><span data-offset-key="bbpvo-0-0"><span data-text="true">aa</span></span></div>
HTML snippet is above, I want to set aa to bb.
Capybara is designed to emulate a user - A user can't edit a span unless there's some sort of javascript widget attached to it. If you have a JS widget attached to the span you would need to perform whatever actions a user would do in order to edit the span. So you say the user has to click on the span and then type on the span - if that is so then you could try something like
span = find('span[data-text="true"]')
span.click
span.send_keys("new content", :enter) # if enter is needed to end the editing
which may work - although I'm going to guess the element actually gets replaced with an input or something after it's clicked on, in which case you need to figure out what those elements are (using the browsers inspector) and then find and use send_keys or set on that element instead
To set text in span value,jquery can be used with capybara as shown below:
page.execute_script("$("<span css selector>").text("testing")");
or
page.execute_script("$("<span css selector>").html("testing <b>1 2 3</b>")");

How do I let Selenium driver click on link which is dynamically created through JavaScript

I am trying to automate actions and unable to select an element due to its dynamic nature.
I am running Selenium web driver on ruby and am trying to select value that is not present in page source.
<a class="linkOtherBrowser" onclick="addChangeStatusField('InitialSelectionPage');submitFormByAction('ChangeStep');return false;" href="#"><div class="processBarElement noSelected">
<div class="whiteBeforeProcessBarTitles"></div>Initial Selection</div>
<div class="endOfElementOfProcessBar"></div></a>
I am trying to select value "Initial Selection" from above.
Could anyone pls help out?
Thanks,
Abhishek
As the HTML is generated by Javascript, You need to inspect the DOM instead of viewsource and write the element locator code accordingly.
Note: In IE, Firefox or Chrome you can press F12 key to see the developer tools and use the inspect element option to check the DOM.
Whatever element is generated dynamically is added in your DOM. WebDriver has capability of clicking on elements are the visible on UI and hence if the generated element is visible to regular user's you can click on the element easily.
To do so, you need to identify the best selector for that newly generated click, could be xpath or css. Once you identify the selector you can consider clicking clicking using following code
WebElement element = driver.findElement(By.xpath("//a[#title='NAME_TITLE']"));
element.click();
OR
WebElement element = driver.findElement(By.css("a[title='NAME_TITLE']"));
element.click();
There are more options within your By.class on picking the element in best way

Is there a way to select a child element inside another child element in Watin

I am trying to select a link/button inside of a form, that is in a div. the way it was made is that there are two links/buttons of the same id, name etc. however they are in different forms, so the code i wanted to use is:
_myTest.Form(Find.ById("PermissionsForm")).Child(Find.ByClass("saveBtn")).Child(Find.ByText("SAVE"));
any help would be appreciated
I think this is what you need:
var button = _myTest.Form("PermissionsForm").Button(Find.ByClass("saveBtn"));
This will lookup the button having the class 'saveBtn' inside the form 'permissionsform' in your browser instance _myTest.
Have a look at this page to decide if you need to have .Button(..) or .Link(...) based on the html tag that is used in your html.
How about building a regex for the element?
Regex could something like this
Regex elementRe = new Regex("id=LnkOk href="http://stackoverflow.com");
Then you can use your code to Click this link. The Click method can be extended to accept Regex if it is not already doing that.
Let me know how this goes or if you need more info.
Cheers,
DM

Resources