I agree that the Selenium is the best web test automation as per current market standard. Since the power of selenium makes your test directly communicate with web browser dom with any window identification dependency like other tools do. Let us start writing our first selenium script with c#.
Before , you start writing your selenium script you first must understand your requirement and what to do and what not to do.
It is always recommend to understand the API before you start writing selenium scripts.
Example Script:
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: You can launch your webBrowser using selenium classes.
Example:
IWebDriver driver=new FirefoxDriver();
driver.Navigate().GoToUrl("http://yuvarajatcts.blogspot.com"));
The above code launches Firefox browser with the Blog page navigated to. If you want to launch the application in chrome then use "driver=new ChromeDriver()"" , in IE driver=new InternetExplorerDriver();.
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. 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:
IWebElement searchTextBox=driver.FindElement(By.Id("searchTextBox"));
Note, the return type of FindElement is IWebElement interface.
Step3:
Once you identify the object, perform action on the control you identifies, such as Mouse Click, Keyboard Press, etc.,
So, I just call
searchEdit.SendKeys("this is sample");
Then, Press enter to wait till the page loads
searchEdit.SendKeys("{Enter}");
So, You can put the above code into TestMethod as follows:
Selenium Test Example:
[TestMethod]
public void TestSelenium()
{
IWebDriver driver=new FirefoxDriver();
driver.Navigate().GoToUrl("http://yuvarajatcts.blogspot.com");
IWebElement element= driver.FindElement(By.Id("searchTextBox"));
element.SendKeys("this is simple");
element.SendKeys("{Enter}");
}
No comments:
Post a Comment