You can start writing codedui test by using the CodedUI API. You need to follow few steps before you start writing CodedUI Tests. One, Understand the API and identify the classes that are used in identify the control objects. Second, Follow a specific programming standard to structure your CodedUI Test Code.
Now, I will walk you through how to write the Automation Test using the CodedUI API.
Let us take a simple scenario to start our first automation test.
Scenario: Search a blog in <yuvarajatcts.blogspot.com> and read the content in the blog.
Design: To start your script, let us just go by considering some manual steps to achieve our goal(reading content).
1. Launch Browser Window.
2. Navigate to the specified URL.
3. Identify the Search text box and type the query.
4. Wait for the post to load and read the content.
Now, let me explain how we write the code automate our manual scenario.
Step1:
Launch WebBrowser: CodedUI is provided with a class "BrowserWindow" which has the different methods to perform actions on the browser. So, to launch a browser we have a method "Launch"
which launches the browser.
As, CodedUI has support for IE (<11), Firefox(26), and Chrome (Latest) browser, we can play-
-back the designed scripts on different browsers by making a setting to CurrentBrowser property.
Example:
BrowserWindow.CurrentBrowser="Chrome";
BrowserWindow browser=BrowserWindow.Launch(new Uri("http://yuvarajatcts.blogspot.com"));
The above code launches chrome browser with the Blog page navigated to. If you want to launch the application in firefox then use "BrowserWindow.CurrentBrowser="firefox"" , in IE BrowserWindow.CurrentBrowser="IE" (Optional to use).
Step2:
Identify the Search text box and type the query. Now let us take the web application and get the search text box object properties. This can be achieved with different tools/ plugins. Some of them are Firebug for Firefox, Inspect Element for chrome and Firefox, Developer Tools for IE, and Test Builder from CodedUI. So, it is up to you based on your convenience and ease.
Assume, you text box has "<input type='text" id="searchTextBox"> </input>
To identify the above text box , you can write the following code:
HtmlDocument document=new HtmlDocument(browser);
HtmlEdit searchEdit=new HtmlEdit(document);
searchEdit.SearchProperties[HtmlEdit.PropertyNames.Id]="searchTextBox";
In the above lines of code, the first line of Code is mandatory which makes your code run on multibrowser. If you omit this lines of Code, your code Script only runs in IE, but fails in Chrome and Firefox browsers.
Step3:
Once you identify the object, perform action on the control you identifies, such as Mouse Click, Keyboard Press, etc.,
In this scenario, as I want to set text to search text box, I use "Text" Property which defined in "HtmlEdit" Class.
So, I just call
searchEdit.WaitForControlReady();
searchEdit.Text="this is sample";
Then, Press enter to wait till the page loads
Keyboard.SendKeys(searchEdit,"{Enter}");
So, You can put the above code into TestMethod as follows:
CodedUI Test Example:
[TestMethod]
public void TestCodedUI()
{
BrowserWindow.CurrentBrowser="Chrome";
BrowserWindow browser=BrowserWindow.Launch(new Uri("http://yuvarajatcts.blogspot.com"));
HtmlDocument document=new HtmlDocument(browser);
HtmlEdit searchEdit=new HtmlEdit(document);
searchEdit.SearchProperties[HtmlEdit.PropertyNames.Id]="searchTextBox";
searchEdit.WaitForControlReady();
searchEdit.Text="this is sample";
Keyboard.SendKeys(searchEdit,"{Enter}");
}
Now, I will walk you through how to write the Automation Test using the CodedUI API.
Let us take a simple scenario to start our first automation test.
Scenario: Search a blog in <yuvarajatcts.blogspot.com> and read the content in the blog.
Design: To start your script, let us just go by considering some manual steps to achieve our goal(reading content).
1. Launch Browser Window.
2. Navigate to the specified URL.
3. Identify the Search text box and type the query.
4. Wait for the post to load and read the content.
Now, let me explain how we write the code automate our manual scenario.
Step1:
Launch WebBrowser: CodedUI is provided with a class "BrowserWindow" which has the different methods to perform actions on the browser. So, to launch a browser we have a method "Launch"
which launches the browser.
As, CodedUI has support for IE (<11), Firefox(26), and Chrome (Latest) browser, we can play-
-back the designed scripts on different browsers by making a setting to CurrentBrowser property.
Example:
BrowserWindow.CurrentBrowser="Chrome";
BrowserWindow browser=BrowserWindow.Launch(new Uri("http://yuvarajatcts.blogspot.com"));
The above code launches chrome browser with the Blog page navigated to. If you want to launch the application in firefox then use "BrowserWindow.CurrentBrowser="firefox"" , in IE BrowserWindow.CurrentBrowser="IE" (Optional to use).
Step2:
Identify the Search text box and type the query. Now let us take the web application and get the search text box object properties. This can be achieved with different tools/ plugins. Some of them are Firebug for Firefox, Inspect Element for chrome and Firefox, Developer Tools for IE, and Test Builder from CodedUI. So, it is up to you based on your convenience and ease.
Assume, you text box has "<input type='text" id="searchTextBox"> </input>
To identify the above text box , you can write the following code:
HtmlDocument document=new HtmlDocument(browser);
HtmlEdit searchEdit=new HtmlEdit(document);
searchEdit.SearchProperties[HtmlEdit.PropertyNames.Id]="searchTextBox";
In the above lines of code, the first line of Code is mandatory which makes your code run on multibrowser. If you omit this lines of Code, your code Script only runs in IE, but fails in Chrome and Firefox browsers.
Step3:
Once you identify the object, perform action on the control you identifies, such as Mouse Click, Keyboard Press, etc.,
In this scenario, as I want to set text to search text box, I use "Text" Property which defined in "HtmlEdit" Class.
So, I just call
searchEdit.WaitForControlReady();
searchEdit.Text="this is sample";
Then, Press enter to wait till the page loads
Keyboard.SendKeys(searchEdit,"{Enter}");
So, You can put the above code into TestMethod as follows:
CodedUI Test Example:
[TestMethod]
public void TestCodedUI()
{
BrowserWindow.CurrentBrowser="Chrome";
BrowserWindow browser=BrowserWindow.Launch(new Uri("http://yuvarajatcts.blogspot.com"));
HtmlDocument document=new HtmlDocument(browser);
HtmlEdit searchEdit=new HtmlEdit(document);
searchEdit.SearchProperties[HtmlEdit.PropertyNames.Id]="searchTextBox";
searchEdit.WaitForControlReady();
searchEdit.Text="this is sample";
Keyboard.SendKeys(searchEdit,"{Enter}");
}
No comments:
Post a Comment