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

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);

Related

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>")");

Find a button on a web page looking for part of its InnerText string in Visual Studio

I am writing automated tests for a website. On the website there is a button that will have the inner text "All Open" + four random digits. For example: "All Open2957". Is there a way to find the button using only the "All Open" part of the string?
This does not work since the string is missing the four last digits:
HtmlSpan uIAllOpenPane = new HtmlSpan();
uIAllOpenPane.SearchProperties[HtmlDiv.PropertyNames.InnerText] = "All Open";
The simple answer is to use the PropertyExpressionOperator.Contains rather than PropertyExpressionOperator.EqualTo comparator and search for just the required text.
For recorded tests, find the control in the UI Map editor and view its properties panel. Click the "Search properties" field and then click the ellipsis. The window that appears allows the comparator and the required text to be altered.
For hand coded tests use code of the form:
uIAllOpenPane.SearchProperties.Add(HtmlDiv.PropertyNames.InnerText,
"All Open",
PropertyExpressionOperator.Contains);
According to this Microsoft blog the array index style (ie with [ and ]) as used in the question internally calls the SearchProperties.Add(...) but that style has no variation to specify ...Contains, so call the ...Add(...) explicitly.
Check this out
Button allOpenButton = (Button)BrowserWindow.ExecuteScript("allOpenButton = function(){var found; $('input[type=\"button\"]').each(function(){ if($(this).val().indexOf('All Open') > -1){ found = $(this);};}); return found;}; return allOpenButton();");
When trying to locate the controller I only got the FailedToPerformActionOnHiddenC‌​ontrolException. Eventually I was able to locate the DIV container that contained the controller I was trying to locate instead of focusing on finding the controller directly. After finding the container I could locate the controller using
HtmlSpan uIAllOpenPane = new HtmlSpan(container);
uIAllOpenPane.SearchProperties.Add(HtmlDiv.PropertyNames.InnerText, "All Open",
PropertyExpressionOperator.Contains);
The code from #AdrianHHH helped with the problem when part of the string is randomized each time you encounter the controller.

How to verify contents are greyed out

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

How to open and handle richtext editor in javascript in sitecore 6.5?

I've been working on a custom field, which contains a list.
I have to be able to edit the selected item on the list in a richtext editor. (this is the only missing part).
I've read the topic on opening from c# code Opening Rich Text Editor in custom field of Sitecore Content Editor .
This works nice for the "add" button, since i have to open the RTE empty(with default text...), but not for the Edit button.
My aproaches are:
Somehow in the Edit button's message field list:edit(id=$Target) pass the selected index (like list:edit(id=$Target,index=$SelectedIndex), but i don't know how to populate $SelectedIndex
Somehow in the overridden HandleMessage method get the list's selected index. I'm able to get the selected value Sitecore.Context.ClientPage.ClientRequest.Form[ID of list], but thats alone not much of a help, since i won't be able to decide which one to edit if two listitem equals.
Do the richtext editor opening and handling fully in javascript. As i saw at some script in content editor, i tried to do that, but i can't understand it clearly:
richtext editor url:
var page = "/sitecore/shell/Controls/Rich Text Editor/EditorPage.aspx";
some params :
var params = "?da=core&id&ed=" + id + "&vs=1&la=en&fld=" + id + "&so&di=0&hdl=H14074466&us=sitecore%5cadmin&mo";
and the part where i'm not sure:
var result = scForm.browser.showModalDialog(page + params, new Array(window), "dialogHeight:650px; dialogWidth:900px;");
This way the RTE opens as expected (i guess i could get the selected index from javascript and pass it as a parameter later). However when i click ok, i get exception from EditorPage.js saveRichText function: Cannot read property 'ownerDocument' of null. Am i missing some parameter?
Either of the three aproaches is fine for me(also i'm open for new better ones) as soon as i'm able to do it.
Thanks in advance!
Tamas
I was able to enter some javascript into the message:
list:Edit(id=$Target,index='+document.getElementById(ID of the select using $Target ).selectedIndex+')
this way i got the index in HandleMessage.
I'm waiting for better solutions now.

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