Generic selenium testing images - selenium

With selenium can one run a test which check all images on a current page if they contain ALT attribute and report images not having it ?

Yes. The first thing you need to do is decide which selenium you want to use.
You can use Selenium IDE, where a test consists of an HTML table.
You can use Selenium RC, where you write a test in a programming language (eg. C#, Java, PHP, Ruby, etc).
The latter is a little more complex at first, but if you want real power, you will use that method.
Then, you'll need to learn how to find all the images on a page.
//img is a good XPath query to use. For Xpath details, see w3schools, and especially this page.
Then you'll want to find images with an alt attribute: //img[#alt]
One approach would be to count how many images there are, and subtract the number with alt attributes.

You can also do this in WebDriver (soon to be Selenium 2). The following example is for TestNG/Java but other client languages are available.
List<WebElement> images = driver.findElements(By.xpath("//img[not(#alt)]"));
assertEquals(images.size(), 0);
For more feedback you could also use something like the following to output details of the images without alt attributes:
for(WebElement image : images) {
System.out.println(image.getAttribute("src"));
}

We have a similar test but we grab the page's Html and parse out the images with a regular expression and then match on a second regular expression looking for the alt tag. I haven't done a speed test, but I think it might be faster than doing the Xpath route.

Related

Selenium Webdriver selecting wrong input in jquery dialog

I am testing a site that has a dialog box with multiple text fields, some with datepickers, some without.
For some reason, when I attempt to locate the inputs by their XPath selenium finds the element, but when it comes time to input data, it send the keys to the top field. Here is an example of my selenium code:
DriverHelper.SendKeysByXpath("//input[#name='registrationStartDate']",CurrentDate);
DriverHelper.SendKeysByXpath("//input[#name='firstStartDate']",CurrentDate);
DriverHelper.SendKeysByXpath("//input[contains(#name,'lastStartDate')]","01/01/2018");
FYI, the DriverHelper is a class I call to save myself from typing the same selenium calls over and over again.
The XPath names are all unique and as far as I know there shouldn't be any confusion as to which input I'd like to send the keys to.
Has anyone run into similar situations before?
I'm trying to get an example up in jsfiddle but so far I'm unable to replicate the issue there.
It is sending keys to the top element probably because that is the one that he is selecting.
Try to use this:
List<WebElement> result = webDriver.findElements( by );
Debug your code and check how many elements your selector is retrieving.
Try to narrow and improve your search in such a way that you narrow the group os selected elements as possible.
Very personal opinion:
The code is easier to fix and to read if you avoid using xPaths as selectors. I prefer id's and classes whenever its possible.
Selenium best pratices source

Identify objects in selenium

In my application, it has several tabs, say 'AAA','ABC','ADF'; I need to automate the click on tab 'ABC'.
Those tabs have ids and they are 'tab1','tab2','tab3'. I can done this easily by using ids. but i don't want to use this because those tabs will change time to time. so I need to use the name in the tab, because it is unique.
Below is my tag:
<a id="tab2" class="current" onclick="expandcontent('sc2', this);" href="#"> ABC </a>
If this is the case, you will probably need to fall back to XPath and perform text-based searches, for instance:
//a[text()='ABC']
Though, I'd advise you work with your development team to have consistent ID's. Text based matching is fine, but when you start to use older browsers you'll notice it really really slows down the tests.
However, you can also use the .LinkText and .PartialLinkText selectors in your language API's - there should be an implementation of those selectors in each API (C#, Ruby, Python etc). The catch here is this will be for a (anchor) elements only. However, providing that is the only type of elements this needs to be done by, you can get away with using this instead of XPath.
As Arran mentioned, you are likely best off searching for the tab names using XPath but, you can also use FindBy as well. An example of this would be:
#FindBy(css=<the CSS value for the tab>) private WebElement pageTab2;
#FindBy(id="tab2") private WebElement pageTab2;
I can only agree with what Arran also mentioned about your developers using more relevant naming conventions as well. The easier they make your job, the more you can do to make their lives easier too.
Simplest answer will be Use..."link=ABC" or link=" ABC " (it there are leading and trailing spaces).
The simplest approach would be to use X paths to find your tabs.
When you are starting out there are two helpful tools to help you find X paths. Download Mozilla Firefox and get these two add-ons:
Web Driver Element Locator-After downloading you can right click a web element and select X path.
Selenium IDE-This is a recorder and will give you the X paths or CSS of each web element you interact with.
I would recommend starting with these tools to help you out since you are just starting out.
As for your question:
I would recommend using what #Arran said...
//a[text()='ABC']
'a'-will search the entire page for whatever text you have in single quotes ''
If you Right Click the element or Tab and select Inspect. You will then be looking through the back-end of the page for classes or div that help you identify each different element.
xpath=//a[contains(#class,"current") and contains(#text,"ABC")]

Finding clickon Element using Selenium. (JAVA)

I spend hours already trying to find the way to find the Element using Selenium WebDriver. I assume I need to use driver.findElement(By.xpath("")), but I am not quite sure how.
I somehow need to find and click on "clickon" element. The problem is that part of that element is changing (see screenshot) I need to pick up from the file and putted into the xpath.
I would appreciate any help.
We have been rigorously searching for automated functional testing solutions recently, and we began with Selenium. The entire reason we decided to search for other solutions was that our application also has dynamic IDs with no other obvious XPath mechanism to identify them. Selenium is unable to identify these elements on the page without some additional knowledge, just as you would be unable to identify these elements on the page if you didn't already know what they are.
If you are controlling the DOM creation, consider adding a unique ID or class to this element.
We recently came across eggPlant from testPlant, and it is an interesting approach to functional testing. It's essentially image based. Other viable solutions are Ranorex or HP's QTP or SmartBear's TestComplete.
You can use xpath. If the div class is constant, you can use something like:
driver.findElement(By.xpath("list-row field-item")).click();
To view the xpath, you can install firefox plugin called 'xpath checker' found here and right click on the dom element and click 'View Xpath' option to get the xpath of the element and then you can use that xpath in your code.
Or you can even use regex in the xpath which is suitable for the similar problems. Xpath with regex is really powerful.
It seems that you want to click the div that has the on click attribute that contains certain text that doesn't change, ignoring the part that does. In that case, use an xpath like this:
//div[contains(#onclick, '/challenge/index/rfp_id/')]
This will select the first div with an onclick attribute with a value containing /challenge/index/rfp_id.

Verifying text is present within certain element

When creating selenium tests using the #{selenium} tag, how do you use complex XPath locators? I've got some simpler examples that work but I'd really like to avoid having to give every element an id just to facilitate testing.
I've tried variations like these:
#{selenium 'Sitemap'}
...
// These work:
assertTitle('Site Map')
verifyTextPresent('Site Map')
verifyTextPresent('Login')
verifyText('id=test', 'Login')
verifyText('//ul', 'Login')
verifyText('//ul[2]', 'Login')
// this one results in "Element //ul[#class=sitemap] not found"
verifyText('Login','//ul[#class=sitemap]')
#{/selenium}
Has anyone gotten the more complex versions working? It looks like it should work according to the selenium docs. Also, is the creation of selenium tests in the context of Play documented anywhere? The only mention of it that I can find are these trivial examples.
I disagree to a degree. Badly constructed xpaths that search through the entire DOM will slow down tests but well constructed xpaths should have minimal effect on speed (unless you are using IE which has a horrific JavaScript rendering engine).
Ideally you want to key your xpath to an ID as close to the area of the DOM you want to search as possible, this will ensure you are only searching a specific area of the DOM rather than using an xpath like the one shown above that will search through the entire DOM even it it does find a matching element quickly.
I'll provide some examples of what I would call good xpaths using http://www.lazeryattack.com as an example. If you have anything specific in mind shout and I'll see what I can do to help:
The Voice Comms link: //ul[#id='leftMenu']/li/a[.='Voice Comms']
H3 element that contains a span element with the text "January VAT Increase": //div[#id='news']/h3[span[.='January VAT Increase']]
The first paragraph of text under the above element: //div[#id='news']/h3[span[.='January VAT Increase']]/following-sibling::p[1]
The second paragraph of text under the above element: //div[#id='news']/h3[span[.='January VAT Increase']]/following-sibling::p[2]
Start searching from the featuredNews div and drop down to the h2 element with the text "New Look And Feel": //div[#id='featuredNews']/descendant::h2[.='New Look And Feel']
I would suggect using FireFox with the FireBug and FirePath extensions to help you work out xpaths, this is my personal favourite combination.
Did you try
//ul[#class='sitemap']
Notice the single quote around sitemap.
XPath would make your tests slow and id, name are better option to use.

Selenium xpath flow

Can anyone tell me what is the exact flow of taking out xpath in Selenium-IDE.
After trying alot by putting alerts i m not getting how to take out the exact xpath.
Selenium displays the xpath according to xpath:position and some others ways also but i want to add the xpath traversing from html i.e the topmost position.How can i do that???
I've come to prefer CSS selectors over Xpath. The IDE won't create them for you, but with a little practice they're very easy to write. As a bonus, they make your tests much easier to maintain.
For example, let's say you have a button with the text "save". The locator would be:
css=button:contains('save')
Check out http://www.w3.org/TR/css3-selectors/ for more detail.
I use XPather to find out the xpath. Then I use the resulting path in the Selenium IDE to cross verify. You could also use other tools like XPath Checker or Firebug to do the same.
Write 1 line of code in javascript:
LocatorBuilders.order = ['id', 'link', 'name', 'dom:name', 'xpath:link', 'xpath:img','xpath:attributes', 'xpath:href', 'dom:index', 'xpath:position'];
change it the way you want (not all of them has to be there)
see:
chrome://selenium-ide/content/locatorBuilders.js
save as your_file.js
add your_file.js as "Selenium Core extensions" in the IDE options
Firefinder for Firebug helps matching both CSS and XPath locators
In general, with Firebug installed, you can Inspect Elements for their HTML and DOM structures to determine what best locator you can build.
the best way to find x PAth is :
open developer tool bar in chrome:
Find the x Path displayed below :
You can directly copy and paste this x path in your script