Tuesday, 7 January 2014

Webdriver commands with examples



WebDriver is a tool for writing automated tests web applications. It aims to mimic the behavior of a real user, and as such interacts with the HTML of the application.
findElement() : Returns a single WebElement within the current page
Syntax: WebElement findElement(By by)
driver.get("https:www.gmail.com");
WebElement userName=driver.findElement(By.id("username"));
userName.sendKeys("ravismundhe");
-- findElements() : Returns List of WebElements within the current page.
Mechanism used to locate elements within a document.
Direct Known Subclasses:
By.ByClassName, By.ByCssSelector, By.ById, By.ByLinkText, By.ByName, By.ByPartialLinkText, By.ByTagName, By.ByXPath, ByAll, ByChained, ByIdOrName
driver.get("https://www.matrix.com");
List <webelement> textboxes1=driver.findElements(By.xpath("//input[@type='text']"));
System.out.println("total textboxes "+textboxes1.size());
driver.get("http://www.google.com"); To open an application
driver.findElement(By.id("passwd-id")); Finding Element using Id
driver.findElement(By.name("passwd")); Finding Element using Name
driver.findElement(By.xpath("//input[@id=’passwd-id’]")); Finding Element using Xpath
element.sendKeys("some text"); To type some data
element.clear(); clear thecontents of a text field or textarea
driver.findElement(By.xpath("//select")); Selecting the value
select.findElements(By.tagName("option")); Selecting the value
select.deselectAll(); This will deselect all OPTIONs from the first SELECT on the page
select.selectByVisibleText("Edam"); select the OPTION withthe displayed text of “Edam”
findElement(By.id("submit")).click(); To click on Any button/Link
driver.switchTo().window("windowName"); Moving from one window to another window
driver.switchTo().frame("frameName"); swing from frame to frame (or into iframes)
driver.switchTo().frame("frameName.0.child"); to access subframes by separating the path with a dot, and you can specify the frame by itsindex too.
driver.switchTo().alert(); Handling Alerts
driver.navigate().to; To Navigate Paeticular URL
driver.navigate().forward(); To Navigate Forward
driver.navigate().back(); To Navigate Backword
driver.close() Closes the current window
driver.quit() Quits the driver and closes every associated window.
driver.switch_to_alert() Switches focus to an alert on the page.
driver.refresh() Refreshes the current page.
driver.implicitly_wait(30) Listing out the different WebDriver Wait statements that can be useful for an effective scripting and can avoid using the Thread.sleep() commands of time to wait
driver.set_script_timeout(30) The amount of time to wait
driver.get_screenshot_as_file The full path you wish to save your screenshot to
driver.get_screenshot_as_base64() Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML

Monday, 6 January 2014

JavaScript Using Variables


JavaScript Using Variables
Variables in JavaScript are basically named buckets of data, a way of creating a reference to that data—regardless of whether the data is a string, number, boolean, array, or other object—so that you can access the same data again and again. More importantly, you can use variables to persist data from one process to another. For instance, your JavaScript application can store the value of a form element in a variable, and manipulate that value without having to actually manipulate the form element itself.

As with algebra, JavaScript variables can be used to hold values (x=5) or expressions (z=x+y).

Variable can have short names (like x and y) or more descriptive names (age, sum, totalvolume).

Variable names must begin with a letter
Variable names can also begin with $ and _ (but we will not use it)
Variable names are case sensitive (y and Y are different variables)
Declaring (Creating) JavaScript Variables
Creating a variable in JavaScript is most often referred to as "declaring" a variable.

You declare JavaScript variables with the var keyword:

Variables in JavaScript behave the same as variables in most popular programming languages (C, C++, etc) do, but in JavaScript you don't have to declare variables before you use them. If you don't know what declaring is, don't worry about it. It isn't important!

When using a variable for the first time it is not necessary to use "var" before the variable name, but it is a good programming practice to make it crystal clear when a variable is being used for the first time in the program. Here we are showing how the same variable can take on different values throughout a script.
JavaScript variables have an identifier, scope, and a specific data type. Because the language is loosely typed, the rest, as they say, is subject to change without notice.

Variables in JavaScript are much like those in any other language; you use them to hold values in such a way that the values can be explicitly accessed in different places in the code. Each has an identifier that is unique to the scope of use (more on this later), consisting of any combination of letters, digits, underscores, and dollar signs. An identifier doesn’t have a required format, other than it must begin with a character, dollar sign, or underscore:

Saturday, 4 January 2014

Working with Java Assertion


Java Assertion or assert keyword in java 
 is little unknown and not many programmer is familiar with this and it's been rarely used specially if you have not writing unit test using JUnit which extensively uses Java assertion to compare test result.
Assertion not only improve stability of code but also help you to become better programmer by forcing you to think about different scenario while writing production quality code and improving your think through ability.
Test your assumptions during development.
Always assert that something is true.
AssertionError is thrown.
Disable by default.
Two different methods:
·         assert     (y &amp;gt; x);
·         assert     (y &amp;gt; x): “y is “+y+” x is “+x;

We were not supposed to make assumptions, but we can’t help it when we’re writing code.
if (x > 2 && y) {
// do something
} else if (x < 2 || y) {
// do something
} else {
// x must be 2
// do something else
}
You write print statements with them:
while (true) {
if (x > 2) {
break;
}
System.out.print(“If we got here something went horribly wrong”);
}
     Added to the Java language beginning with version 1.4, assertions let we test our
assumptions during development, without the expense (in both your time and program overhead) of writing exception handlers for exceptions that we assume will never happen once the program is out of development and fully deployed.

java -ea com.testing.TestClass
or
java -enableassertions com.testing.TestClass
java -da com.testing.TestClass
or
java -disableassertions com.testing.TestClass
no arguments
package names
class names
Don’t use assertions to validate arguments to a Public Method.
Use assertions to validate arguments to a Private Method.
Don’t use to validate command-line arguments.
Don’t use assert expressions can cause side effects.


Thursday, 2 January 2014

Maintaining The Synchronization Point


Synchronization Point instructs QTP to pause until an object property achieves a specific value.
We use “Synchronization Point” to maintain the time coordination between testing process and our application process.
Basically there are 4 ways

 “sync” is more dynamic than wait. In wait we have to wait for specified time even if the process has been completed in less time but with “sync” the page will move on after completion of a process. It is very good to apply but unfortunately we can’t apply it in desktop application.
Example:
 Browser("Google").Page("TV9 Network, Telugu").Sync



This statement instruct QTP to stay (waits) on the same page for specified 'n' seconds even though process has occurred in less than n seconds
we can apply “wait()” by editing the code manually in expert view after a test is recorded.
Example:
For Web Application: wait(10) ….. it will stay on the same page for 10 seconds
For Desktop Application: same as above

Syntax: Browser("Browser").Page("Page").WebElement("WebElement").WaitProperty "property name", property value, timeout(time in milliseconds)
For Desktop:
Window("Window").WinObject("Window Object").WaitProperty property name", property value, timeout(time in miliseconds)
 Here wait property contains 3 things as you can see above, the Property Name list contains the test object properties associated with the object. Select the property which you want to use for the Synchronization Point.
Next comes the Property value for which QTP should wait before continuing to the next step in the test.
Then enter the Synchronization Point timeout (in milliseconds) after which QTP should continue to the next step, even if the specified property value was not achieved or if the specified value was achieved in less time even then it will wait for the specified milliseconds.
How to Apply: Synchronization point can be added while recording without writing any code from “Insert  Synchronization point” it will open your web page,
ü  here click the object  “object selection – Synchronization Point”
ü  pop up opens here
ü  select the object
ü  after clicking on any object “Add Synchronization Point” pop up opens
ü  Here select property name, enter property value and timeout.
We can also add wait property by editing in expert view after a test is recorded.
Example:
For web Application: Browser("Google").Page("Tv9 Network, Telugu").WebElement("WebElement").WaitProperty "abs_x", page, 50000
For Desktop Application: Window("Flight Reservation").WinObject("Insert Done").WaitProperty "text", text,30000

NOTE: There is a mistake done by beginners is to apply and try to find synchronization point after stopping the recording.


You can enter Exist and/or Wait statements to instruct QuickTest to wait for a window to open or an object to appear. Exist statements return a boolean value indicating whether or not an object currently exists. Wait statements instruct QuickTest to wait a specified amount of time before proceeding to the next step. You can combine these statements within a loop to instruct QuickTest to wait until the object exists before continuing with the test.
For example, the following statements instruct QuickTest to wait up to 20 seconds for the Flights Table dialog box to open.
x=Window("Flight Reservation").Dialog("Flights Table").Exist
counter=1
While Not x
Wait (2)
x=Window("Flight Reservation").Dialog("Flights Table").Exist
Counter=counter+1
If counter=10 then
x=True
End if
Wend