Test case automation - automation

I am new to test automation and I need help of experts who can help me in proceeding with the current difficulties.
Currently there is a web browser application which is tested manually based on the test cases in an excel.
There is also an automation framework also which uses Selenium and uses WebDriver and runs on Google Chrome.
The test cases(in excel) used for manual are taken up and another set of test cases(in excel) are written which is nothing but the div elements and the action which the framework should do like click or find which the framework will understand.
1.First I need to manually find out each div id for all the elements and put it in excel which the framework understands.How can I avoid this?
2.Also a new version of the application has come in which all the div id for the elements differ.Hence its pain to note the div id again and put them in excel.
How can I write the test cases only once for each case even if the div changes?
Please help.

Follow a design pattern, e.g. Page Objects

If ids will be changed try to use css and xpath selectors that do not stick to ids. The main idea is to specify such selectors that allow tests to find elements on the page using knowledge by their parents, tag names, other attributes that won't change (class and so on).

Related

OpenTest support for Dojo toolkit

I'm trying to use OpenTest with web applications created with IBM EGL using the Dojo toolkit. The issue with dojo is that it dynamically generates id's every time so they cannot be used as a locator. In addition many elements do not have an xpath so that can't be used either.
It seems like this is a common issue when I search for "dojo" and "selenium" but I haven't found any solutions yet.
Other testing tools have "explicit" support for specific frameworks (e.g. like dojo) so I assume it's technically feasible.
Here is an excerpt from a website where this same question was asked and OpenTest supports building out macros that do just what this indvidual was able to do with .NET code. Please reference the blockquote below as well as the source
I use Selenium to Test my web application which is built by dojo/dijit
and asp.net MVC, so far it works fine.
I've faced the same issues with yours before. My way is "don't think
about dojo widgets" when writing steps interacting with them. Treat
them as normal complex html elements. You need to browse your dom tree
on the client after dojo parse your widgets, locate the real dom
element which dijit's value node or interactive part corresponding to
and do thing (Click, SendKey or GetId in your case) to it.
It is also good to wrap some common actions to widgets into Helpers
which can be reused in your project.
Below is a simple .NET example I use to test whether a button exist in
a dGrid, I just use css selector to find the cell, hope it helps:
[Then("I can delete it at row '(.*)'")]
public void Then_I_can_delete_it_at_row(int rowIndex)
{
var nthRow = Browser.FindElementsChecked(By.CssSelector(".dgrid-content .dgrid-row-table")).ElementAt(rowIndex - 1);
var deleteBtnsInRow = nthRow.FindElementsChecked(By.XPath(".//span[text() = 'Delete']"));
Assert.AreEqual(1, deleteBtnsInRow.Count);
}

Automation of test case independent of ui changes

We are automating test cases for hardware devices like tv, raspberrypi etc which require to perform certain number of steps and check expected result at the end of each step. To automate these test cases we are using dom element attributes such as id, class,data-component-id etc to fetch the objects and perform actions.
The problem with this way of automation is every time UI changes we need to change the dom elements id, class etc and hence it is rework of the scripts.
I want to know if there is automation framework or any other way using which we can automate test cases independent of ui changes.
Instead of using the exact path, try to identify the element in a more generic way. Using "contains" will help you to identify the elements with the text attributes with out going into the way it is defined. So even the DOM structure changes but with the text being same, your tests will not fail.
Contains
It is very handy XPath Selenium locator and sometimes it saves the life of a test automation engineer. When an attribute of an element is dynamic, then you can use contains() for the constant part of the web element but also you can use contains() in any condition when you need.
Syntax: //tag[contains(#attribute, ‘value‘)]
Example: //input[contains(#id, ‘er-messa’)]
Examples:
Java
1
2
3
4
5
6
7
8
//*[contains(#name,'btnClk')]
--> It searches "btnClk" for all name attributes in the DOM.
//*[contains(text(),'here')]
--> It searches the text "here" in the DOM.
//*[contains(#href,'swtestacademy.com')]
--> It searches "swtestacademy.com" link in the DOM.
That's why you should use id selectors if possible. They are supposed to be unique and hence immune to UI changes. The name selector should work the same way.
If you are using Selenium, use the following list of object locators in (loosely) this order of preference:
id
name
tag name
class
CSS or Xpath
linktext or partial linktext
And if you really want the elements to be consistently identifiable, you might talk to the developers and ask them nicely to add the ids.

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.

How can I write Selenium test case for a specific GWT Widget?

I'm very new to Selenium. I want to write Selenium test cases for GWT widget. I can wirte test case for HTML elements since they have id, but i'm not able to do the same in GWT. I want to test widgets such as textboxes, images, listbox etc.
Can anyone help me?
Thanks in advance,
Gnik
Now I set debugId for the GWT widgets. Using that Id I can access the elements and test.
For eg. In UiBinder set id as <g:TextBox ui:field="textBox" debugId="userBox"
In the java code, textBox.ensureDebugId("userBox");
I can access these widgets in Selenium as follows,
selenium.type("gwt-debug-userBox", "testing");
I don't know if selenium is the right tool to test GWT Widgets, I think there are special tools for that.
With Selenium you can automate the browser. If GWT doesn't provide ids for its html elements there are various other ways to locate your elements. For example you can use xpath or css selectors. Just check the Selenium documentation.
But again, be careful about what you want to test. You don't want to test that GWT Widgets create a proper Web application, that is done already. You probably want to use a special GWT test tool where you test the java side of your widget.