How to count elements on web page? - selenium-webdriver

Open one web page say gmail.
Want to count that how many text field or buttons or checkbox or hyperlinks or other html elements are present.

The solution is very simple . This can be done using simple XPath.
Find the type of element count you need.
eg for text fields: xpath can be : input[type='text']
for radio buttons: xpath can be : //input[#type='radio']
for check box buttons: xpath can be : //input[#type='checkbox']
so find all the elements in the page by using simple command using the above xpath:
webdriver.findElements(By.xpath("REQUIRED_XPATH")).size();
will give you the number of elements in the particular web page.

I am using c#. If you use c# as well the followings is the something you want to use. And you decide what tags you need inside the selector. I am using all the tags so far.
ReadOnlyCollection<IWebElement> webElements = Driver.FindElements(By.CssSelector("input, select, textarea, a, button"))//and keep adding
//then do a simple count. The trick here is the selector and you need to make sure you are adding all the tag names are being used in your application
webElements.Count();

If you want to count all elements of a page, you can simply use * for that as shown below.
List<WebElement> items = driver.findElements(By.cssSelector("*"));
System.out.println(items);

Here is how to know instantly how many items in the list on the website, for example, languages in the wiki, using right-click "Inspect element".
Screenshot

Related

Ionic search bar

I've 3 jsons files linked with 3 controllers, and I need to search on them using ionic/angularjs. I would like the results appear as dropdown menu, like Facebook search or like here in Stackoverflow.
Thank you.
So there are two things here that you want to do:
Be able to search inside JSON file, I think you can do that, there is a SO thread for same : Angular Search for value in JSON and display corresponding data
Display an Auto-complete drop-down, I think there bunch of directives out there, like: https://www.versioneye.com/javascript/sn0opr:ionic-autocomplete/0.1
You can also check following Ionic forum thread for type ahead recommendation:
https://forum.ionicframework.com/t/recomendation-for-a-typeahead-widget/55
If you want to create your own have look at following tutorial:
http://www.sitepoint.com/creating-a-typeahead-widget-with-angularjs/

robotframework with sencha extjs

I will use robotframework to test an application that use the sencha extjs library,
my problem is that with this library the ids are generated dynamically every time
this add new components, therefore my robotframework script would change
constantly but this is a bad idea. somebody said me that other testing frameworks
it have plugins to this task but I cant found in robotframework.
thanks in advance.
There are many ways to access elements on the page, using the id is just one way. Assuming you're using Selenium2Library, you can use any of the following locators strategies (from the Selenium2Library documentation):
Strategy Example Description
--------------- --------------------------------------- ---------------------------------
identifier Click Element | identifier=my_element Matches by #id or #name attribute
id Click Element | id=my_element Matches by #id attribute
name Click Element | name=my_element Matches by #name attribute
xpath Click Element | xpath=//div[#id='my_element'] Matches with arbitrary XPath expression
dom Click Element | dom=document.images[56] Matches with arbitrary DOM express
link Click Element | link=My Link Matches anchor elements by their link text
partial link Click Element | partial link=y Lin Matches anchor elements by their partial link text
css Click Element | css=div.my_class Matches by CSS selector
jquery Click Element | jquery=div.my_class Matches by jQuery/sizzle selector
sizzle Click Element | sizzle=div.my_class Matches by jQuery/sizzle selector
tag Click Element | tag=div Matches by HTML tag name
default* Click Link | default=page?a=b Matches key attributes with value after first '='
Note that even though some examples (such as xpath) show the use of an id, an id may not be strictly required (except, obviously, for id= and identifier=). xpath is usually the strategy-of-last-resort, because you can reference pretty much anything in the document. For more about xpath you can start here: http://en.wikipedia.org/wiki/XPath
This is not a great solution, so hopefully someone else can find a plugin or something that is easy to implement. However, if you do not find a different way, you can give every one of your Ext.js elements an ID manually by setting the "id" property on every one of your elements to a unique ID yourself. It would not take too much work, and if you named them something actually related to what they are, it would make the ID's more human readable.
From the Sencha docs:
id : String
The unique id of this component instance.
It should not be necessary to use this configuration except for singleton objects in your application. Components created with an id may be >accessed globally using Ext.getCmp.
Instead of using assigned ids, use the itemId config, and ComponentQuery which provides selector-based searching for Sencha Components analogous to DOM querying. The Container class contains shortcut methods to query its descendant Components by selector.
Note that this id will also be used as the element id for the containing HTML element that is rendered to the page for this component. This allows you to write id-based CSS rules to style the specific instance of this component uniquely, and also to select sub-elements using this component's id as the parent.
Source: http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.AbstractComponent-cfg-id
With the release of Selenium2Library 1.7, users are now able to create their own custom locators. You could use this to create a location scheme based on ext's component query. So long as the overall structure of the app doesn't change that much, that may suffice. For more information on custom locators see the Selenium2Library docs.
I've started an add-on library for S2L on github that you can check out for reference, but it isn't completely finished yet.
Also, Ext lets you place unique ids on components by simply specifying the id attribute. I would recommend applying ids to all elements which you want to interact with through your tests, thereby eliminating any need for complex behavior to automate the interface.

outputting checkbox ids to an empty div

I am currently building a keyword bank to assist in tagging my in-house design assets. My idea is to have each keyword as a checkbox form element with one empty div at the bottom. You'd go through the entire list, check what you need and the text assigned to the id of that checkbox would populate in the empty div, separated by commas, and I can just copy/paste that text right into the metadata. I plan on having hundreds, so I'm hoping there is simple solution to this.
I have no problem setting up the html form/checkboxes, but I am unsure how to make this text populate in that empty div—or even IF it's possible. Would I use jQuery? Ajax? I also don't need a submit button—just that div to update immediately each time a checkbox is touched. Would I also need to have some server-side communication with a submit button to even make this work?
Thank you so much in advance for any help you can give or any direction you can point me in.
Here is a working fiddle
What you need is basically something like this :
$( ":checkbox" ).click(function() {
//alert($( this ).val());
$("#myText").append($(this).val() + ";");
});
You can forget the alert. This will add the value assigned to the checkbox in the div followed with a coma. :)

Overview page of elements from a Typo3 page tree

I'm primarily a UI and graphic designer and, eventhough I have some experience with Typo3, I'm completely stuck at the following problem:
I have a large page tree with single pages for items from a catalogue (one item per page), the layouts for these items are built with Armin Vieweg's beautiful "Dynamic Content Elements" extension (DCE).
Now I want to create an overview page where I reference some of those items automatically - ideally I want to check a box in each element I want to display there (I would add a field catalogueItemPreview to the item DCE which authors can check or uncheck).
Unfortunately, I have no concrete idea of how the database is structured and how I could build a query (where would I even do that? in a custom-made plugin?).
This is how I imagine it could work: On the overview page I use a plugin/an extension in a Content Element that does the following:
search Typo3 DB for content elements with a field called "catalogueItemPreview"
return fields "catalogueItemTitle", "catalogueItemShortDescription", "cataloguePreviewImage"
use a template to render previews of all those elements on the overview page
I'm happy for ANY pointers towards a solution as currently I'm completely in the dark about where even to begin ...
Schematic screenshot from the Typo3 backend
thanks for using my DCE extension :)
Well the fields you have defined and their values in content elements are stored as XML, because the current version of DCE is based on Flexforms.
This makes it very very difficult to do MySQL queries using one of the field properties in WHERE clause. You could check for a xml string in the field pi_flexform but this is not recommended.
Instead I would use another property of content elements (tt_content) to mark the items as "Show on frontpage". For example you could create a new layout or section_frame value for that. Then it is very easy to just output the elements you want, using TypoScript.

Render a page differently based on which list that opened it

I have two lists of different PageTypes - NewsItems and PressReleases. They are displayed in one list each, with links to the individual items.
Now I want to include the press release items into the news list and use the style of the news items to display them as news items. They share properties like "Heading" and "BodyText", which are used in the News Template.
I imagine that it won't be that difficult to feed the NewsItems' ListPage with both sets of pages, but I don't understand how I can control the rendering of the item page.
I would like to take the PageData object from a NewsItem OR a PressReleaseItem and display it using the News-Item.aspx template, if it is selected in a NewsList. But EPiServer will always render the PressReleaseItem with the PR-Item.aspx since it's coupled in the PageType settings.
Anyone know how to accomplish this?
EDIT: An effort to clarify:
The important issue is how to know the "list parent" and choose the right template from that. In the ListPage I can apply different looks on the PR and News items respectively using tompipes answer, but when selecting to see an individual item EPi will render the PR-Item-1 the same way regardless of their "list parent". That's the problem.
I'm not following exactly what you are attempting here. But I think I get the gist of it.
Why not use one aspx template for both page types, but in the code behind switch off sections using the visible attribute.
If you are using PageTypeBuilder you could use the "is" keyword:
somePlaceHolder.Visible = CurrentPage is NewsItemList;
If you're not using PTB, you could use something like:
somePlaceholder.Visble = CurrentPage.PageTypeID == 10;
or
somePlaceholder.Visble = CurrentPage.PageTypeName == "NewsItemList";
I'll point out now I'm not a fan of hardcoding anything, so I would place the template name, or ID into a config file, or a property on the start/root page to avoid hardcoding them.
Let me know if this will help, or if I have misunderstood please try elaborate on your issue.
Depending on how much the templates share you could go with user controls, placeholders or even different masterpages to switch view in a suitable way.
To know when to switch you could use a querystring parameter, session variable or the nicest looking way would probably be to lookup and get the list's PageData object by the HTTP referrer. If it's empty you will get press release rendering as the worst case.
I tried lots of solutions, including the adding of querystring to the PR items in the list links, the getting of referring url in the item template and different types of event hooking for automatic publishing of news items from a PR item (although I only looked at the code samples for that one), and finally came to the conclusion that they all had something that told me not to go that way. (Making the code too complex, or the markup logic too hard to understand and so forth)
I ended up using Fetch data from another EPiServer page, and creating a "shortcut pagetype" in which I let my editors pick which PR item should be used as base for a news item.
This shortcut pagetype is called "PR-as-news-itemPage" and it is rendered with the same aspx as ordinary news items: News-Item.aspx. Having no properties of its own, it will take all relevant data from the PR item selected with "Fetch..."
To render PR items with all its properties I created an ordinary new pagetype called PR-Item.aspx. This renders the "Attribute 2" property, which is only rendered by PR-item.aspx, and not by News-Item.aspx.
(I could have gone even simpler, by letting the editors use the old News-Item page type and use the "Fetch..." property there, but I have some mandatory properties in that page type which I didn't want to make optional for this sake.)

Resources