Code & QA

Test with the web elements

 

This test checks textbox field located on New tab page. I want to write down term ‘jobs’ into the field and perform search by this term with Google search service (button Search with Google does it). Then I return back to New tab page and extend the search term with ‘steve’ word so it is ‘jobs steve’. And perform search. Expected results: 1) page title is ‘jobs – Google Search’, 2) page title is ‘jobs steve – Google Search’.

Selenium Webdriver tool has special methods for navigating by browser history with back and forward buttons. They are driver.Navigate().Back() and driver.Navigate().Forward().

I wrapped the methods with my own ones (for having logging) and put them into WebApplication class since the WebApplication is a reflection of the tested site opened in a browser.

/// <summary>

/// Navigates step back in browser history

/// </summary>

public void Backward()

{

Driver.Navigate().Back();

Report.AddInfo(“Navigating backward”, string.Empty, Driver.TakeScreenshot(“Navigating backward”));

}

/// <summary>

/// Navigates one step forward in browser history

/// </summary>

public void Forward()

{

Driver.Navigate().Forward();

Report.AddInfo(“Navigating forward”, string.Empty, Driver.TakeScreenshot(“Navigating forward”));

}

At the moment I do not have New tab page described in the framework. So I have to do it and then to add the page to the WebApplication class.

NewTabPage.cs:

using Mapping.WebElements;

using OpenQA.Selenium;

namespace Mapping.TestingWithSelenium

{

public class NewTabPage : WebPage //this class is not inherited from MasterPage

{

public NewTabPage(IWebDriver driver) : base(driver) { }

public override bool IsLoaded

{

get

{

try

{

return (TextboxSearch.IsDisplayed && ButtonSearch.IsDisplayed);

}

catch { return false; }

}

}

#region Elements

private WebLabel _description;

private WebTextbox _searchForm;

private WebButton _searchButton;

private WebButton _closeButton;

/// <summary>

/// Text in the top of the page which describes page purpose

/// </summary>

public WebLabel LabelDescription

{

get

{

if (_description == null || !WebApplication.IsValid)

{

_description = new WebLabel(Driver, “page description text”, locators: new ElementLocator(By.TagName(“p”)));

}

return _description;

}

}

/// <summary>

/// Search form textbox

/// </summary>

public WebTextbox TextboxSearch

{

get

{

if (_searchForm == null || !WebApplication.IsValid)

{

_searchForm = new WebTextbox(Driver, “Search”, locators: new ElementLocator(By.Id(“searchQuery”)));

}

return _searchForm;

}

}

/// <summary>

/// Search with Google button

/// </summary>

public WebButton ButtonSearch

{

get

{

if (_searchButton == null || !WebApplication.IsValid)

{

_searchButton = new WebButton(Driver, “Search”, locators: new ElementLocator(By.XPath(“//button[contains(.,’Search’)]”)));

}

return _searchButton;

}

}

/// <summary>

/// Close button

/// </summary>

public WebButton ButtonClose

{

get

{

if (_closeButton == null || !WebApplication.IsValid)

{

_closeButton = new WebButton(Driver, “Close”, locators: new ElementLocator(By.XPath(“//button[text()=’Close’]”)));

_closeButton.ClickValidator = new[] { ClickValidator.NumberOfWindowsChanged };

}

return _closeButton;

}

}

/// <summary>

/// Searct images only checkbox

/// </summary>

public WebCheckbox CheckboxSearchImagesOnly

{

get

{

if (_searchImagesOnlyCheckbox == null || !WebApplication.IsValid)

_searchImagesOnlyCheckbox = new WebCheckbox(Driver, “Search images only”, locators: new ElementLocator(By.Id(“img-only”)));

return _searchImagesOnlyCheckbox;

}

}

#endregion

}

}

WebApplication class:

using Logger;

using Mapping.TestingWithSelenium;

using OpenQA.Selenium;

namespace Mapping

{

public class WebApplication

{

/ . . . /

#region Pages

private HomePage _homePage;

private GalleryPage _galleryPage;

private NewTabPage _newTabPage;

public HomePage HomePage

{

/ . . . / }

}

public GalleryPage GalleryPage

{

/ . . . /

}

public NewTabPage NewTabPage

{

get

{

if (_isNew)

Open<NewTabPage>(); if (!IsValid || _newTabPage == null)

{

_newTabPage = new NewTabPage(Driver) { Name = “New tab” };

IsValid = true;

}

return _newTabPage;

}

}

#endregion Pages

public void Open<T>(params object[] args) where T : IWebPage

{

_isNew = false;

if (typeof(T) == typeof(HomePage))

{

Report.AddInfo(“Navigating to Home page”);

Driver.Navigate().GoToUrl(Globals.applicationURL);

_homePage = HomePage;

}

else if (typeof(T) == typeof(GalleryPage))

{

Open<HomePage>();

Report.AddInfo(“Navigating to Gallery page”);

Driver.FindElement(By.XPath(“.//a[text()=’Gallery’]”)).Click();

_galleryPage = GalleryPage;

}

else if (typeof(T) == typeof(NewTabPage))

{

Open<HomePage>();

Report.AddInfo(“Navigating to New tab page”);

_homePage.NewTabMenuItem.Click();

_newTabPage = NewTabPage;

}

}

/ . . . /

}

}

HomePage class has no NewTabMenuItem element yet, so one more to add the item to it. But since the HomePage is inherited from MasterPage page and the navigation menu is available for a lot of pages of the application there is a sense to put menu elements into MasterPage class. Though I still want to update HomePage.IsLoaded property.

using Mapping.WebElements;

using OpenQA.Selenium;

namespace Mapping.TestingWithSelenium

{

public class HomePage : MasterPage

{

public HomePage(IWebDriver driver) : base(driver) { }

public override bool IsLoaded

{

get

{

try

{

return (base.IsLoaded && Paragraph_1.IsDisplayed);

}

catch { return false; }

}

}

#region Elements

WebLabel _p1;

WebLink _linkLearnSeleniumTesting;

/// <summary>

/// First paragraph of page text

/// </summary>

public WebLabel Paragraph_1

{

get

{

if (_p1 == null || !WebApplication.IsValid)

{

_p1 = new WebLabel(Driver, “paragraph 1”, locators: new ElementLocator(new[] { “innerContent” }, By.XPath(“//p[1]”)));

} return _p1;

}

}

public WebLink LinkLearnSeleniumTesting

{

get

{

if (_linkLearnSeleniumTesting == null || !WebApplication.IsValid)

{

_linkLearnSeleniumTesting = new WebLink(Driver, “Learn Selenium Testing”, locators: new ElementLocator(new[] { “innerContent” }, By.CssSelector(“#container a”)));

}

return _linkLearnSeleniumTesting;

}

}

#endregion Elements

}

}

MasterPage.cs (i also added some other elements to the class and changed its IsLoaded property to be correspondent to the framework style):

using Mapping.WebElements;

using OpenQA.Selenium;

namespace Mapping.TestingWithSelenium

{

public abstract class MasterPage : WebPage

{

public MasterPage(IWebDriver driver) : base(driver) { }

public override bool IsLoaded

{

get

{

try

{

return (LabelTitle.IsDisplayed && MenuItemHome.IsDisplayed && IframeInnerContent.IsDisplayed);

}

catch

{

return false;

}

}

}

#region Elements

WebLabel _labelTitle;

WebContainer _iframeInnerContent;

public WebLabel LabelTitle

{

get

{

if (_labelTitle == null || !WebApplication.IsValid)

_labelTitle = new WebLabel(Driver, “Title”, locators: new ElementLocator(By.XPath(“//div[contains(.,’Testing with Selenium’) and not(div)]”)));

return _labelTitle;

}

}

public WebContainer IframeInnerContent

{

get

{

if (_iframeInnerContent == null || !WebApplication.IsValid)

_iframeInnerContent = new WebContainer(Driver, “inner content container”, locators: new ElementLocator(By.Id(“innerContent”)));

return _iframeInnerContent;

}

}

#region Left-side navigation menu

WebLink _menuItemHome;

WebLink _menuItemIFrame;

WebLink _menuItemNewTab;

WebLink _menuItemSandbox;

WebLink _menuItemGallery;

public WebLink MenuItemHome

{

get

{

if (_menuItemHome == null || !WebApplication.IsValid)

_menuItemHome = new WebLink(Driver, “menu item Home”, locators: new ElementLocator(By.XPath(“//a[text()=’Home’]”)));

return _menuItemHome;

}

}

public WebLink MenuItemIFrame

{

get

{

if (_menuItemIFrame == null || !WebApplication.IsValid)

_menuItemIFrame = new WebLink(Driver, “menu item iFrame”, locators: new ElementLocator(By.XPath(“//a[text()=’iFrame’]”)));

return _menuItemIFrame;

}

}

public WebLink MenuItemNewTab

{

get

{

if (_menuItemNewTab == null || !WebApplication.IsValid)

_menuItemNewTab = new WebLink(Driver, “menu item New tab”, locators: new ElementLocator(By.XPath(“//a[text()=’New tab’]”)));

return _menuItemNewTab;

}

}

public WebLink MenuItemSandbox

{

get

{

if (_menuItemSandbox == null || !WebApplication.IsValid)

_menuItemSandbox = new WebLink(Driver, “menu item Sandbox”, locators: new ElementLocator(By.XPath(“//a[text()=’Sandbox’]”)));

return _menuItemSandbox;

}

}

public WebLink MenuItemGallery

{

get

{

if (_menuItemGallery == null || !WebApplication.IsValid)

_menuItemGallery = new WebLink(Driver, “menu item Gallery”, locators: new ElementLocator(By.XPath(“//a[text()=’Gallery’]”)));

return _menuItemGallery;

}

}

#endregion Left-side navigation menu

#endregion Elements

}

}

Code of test:

[Test]

[TestMethod]

public void SearchWithGoogle()

{

#region Test data

const string searchTerm1 = “jobs”;

const string searchTerm2 = “steve”;

#endregion

var testScope = new Dictionary<string, string> {

{ “Perform search with ” + searchTerm1 + ” term on New tab page”, “title of the page is ” + searchTerm1 + ” – Google Search” },

{ “Perform search with ” + searchTerm1 + ” ” + searchTerm2 + ” term on New tab page”, “Title of the page is ” + searchTerm1 + ” ” + searchTerm2 + ” – Google Search” },

{ “Close New tab page”, “Home page is displayed” }};

Report.StartTestCase(GetType().Name, MethodBase.GetCurrentMethod().Name, “Verification of search with Google”, testScope);

Report.RunStep(“Step 1: Search with ” + searchTerm1 + ” term”);

TestedApplication.NewTabPage.TextboxSearch.TypeText(searchTerm1);

TestedApplication.NewTabPage.ButtonSearch.Click();

nUnit.Assert.IsTrue(driver.Title == searchTerm1 + ” – Google Search”);

Report.AddInfo(testScope.ElementAt(0).Key, testScope.ElementAt(0).Value, driver.TakeScreenshot(testScope.ElementAt(0).Value));

Report.RunStep(“Step 2: Search with ” + searchTerm1 + ” ” + searchTerm2 + ” term”);

TestedApplication.Backward();

TestedApplication.NewTabPage.TextboxSearch.AppendText(” ” + searchTerm2);

TestedApplication.NewTabPage.CheckboxSearchImagesOnly.Select();

TestedApplication.NewTabPage.ButtonSearch.Click();

nUnit.Assert.IsTrue(driver.Title == searchTerm1 + ” ” + searchTerm2 + ” – Google Search”);

Report.AddInfo(testScope.ElementAt(1).Key, testScope.ElementAt(1).Value, driver.TakeScreenshot(testScope.ElementAt(1).Value));

Report.RunStep(“Step 3: Close New tab page”);

TestedApplication.Backward();

TestedApplication.NewTabPage.ButtonClose.Click();

nUnit.Assert.IsTrue(TestedApplication.HomePage.IsLoaded);

}

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Undefined offset: 0 in /home/chyqruxavcln/public_html/learnseleniumtesting.com/wp-content/plugins/cardoza-facebook-like-box/cardoza_facebook_like_box.php on line 924