07 - vysnovky z tdd, pohliad pochatkivtsia - vitalii zinchenko it event 2013 (5)

Download 07 -  vysnovky z tdd, pohliad pochatkivtsia - vitalii zinchenko it event 2013 (5)

Post on 05-Dec-2014

368 views

Category:

Education

3 download

DESCRIPTION

07 - TDD, - - IT Event 2013 (5) , : - ; - ; - ; - ; - ; , - . http://itevent.if.ua/lecture/visnovki-z-tdd-poglyad-pochatkivtsya

TRANSCRIPT

1. TDD zinchenko.vitaliy@gmail.com 2. ?: http://xpinjection.com Einar Ingebrigtsen Jasson Huggins Simon Steward 3. 4. 5. ? , - ? - ?: 6. - --: 7. : 8. - , 9. :Unit Test, Visual Studio 2012 + NuGetWebDriver + Selenium 10. BaseObjectusing OpenQA.Selenium;using Selenium;Using OpenQA.Selenium.Support.PageObjects;Using OpenQA.Selenium.Support.UI;namespace FaceBookTest{public class BaseObject{public static IWebDriver Driver;public static DefaultSelenium Selenium;public static void InitPage(T pageClass) where T : BaseObject{PageFactory.InitElements(Driver, pageClass);}}} 11. BaseTestusing Nunit.Framework; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.IE;using OpenQA.Selenium.Chrome; using Selenium;namespace FaceBookTest{ public class BaseTest : BaseObject{[TestFixtureSetUp]public void beforeClass(){Driver = new FirefoxDriver();//for IE Driver = new InternetExplorerDriver(@"");Selenium = new WebDriverBackedSelenium(Driver, TestTools.homePage);Selenium.Start()}[SetUp]public void beforeTest(){Driver.Navigate().GoToUrl(TestTools.homePage);}[TestFixtureTearDown]public void afterClass(){Driver.Close();Selenium.Close();} } } 12. TestToolsusing NUnit.Framework;namespace FaceBookTest{ class TestTools : BaseObject{public static string homePage = "http://www.facebook.com";private const int DEFAULT_TIMEOUT = 60;public static void WaitingForElement(string locator, int timeOut =DEFAULT_TIMEOUT) {for (int i = 0; i < timeOut; i++){if (Selenium.IsElementPresent(locator)){return;}else{System.Threading.Thread.Sleep(1000);}}Assert.Fail("Element with " + locator + " not found");} } } 13. Login-public class LoginPage : BaseObjectpublic const string USER_NAME_FIELD = "//input[@id=email]";public const string PASSWORD_FIELD = "//input[@id=pass]";public const string LOGIN_BUTTON = "//label[@id=loginbutton]/input";public const string FAILED_LOGIN_TEXT = "//div/p[contains(text(),The passwordyou entered is incorrect)]";[FindsBy(How = How.XPath,Using = USER_NAME_FIELD)]public IWebElement userNameField;[FindsBy(How = How.XPath, Using = PASSWORD_FIELD)]public IWebElement passwordField;[FindsBy(How = How.XPath, Using = LOGIN_BUTTON)]public IWebElement loginButton; 14. Login-public static LoginPage GetLoginPage() {LoginPage loginPage = new LoginPage();InitPage(loginPage);return loginPage; }public LoginPage EnterUserName(string userName){userNameField.SendKeys(userName);return GetLoginPage(); }public LoginPage EnterPassword(string password){passwordField.SendKeys(password);return GetLoginPage(); }public UserHomePage ClickLoginButton() {loginButton.Click();return UserHomePage.GetUserHomePage(); }public LoginPage IsLoginFailed(){TestTools.WaitingForElement(FAILED_LOGIN_TEXT);return GetLoginPage();} 15. Login-namespace FaceBookTest{ class LoginPageTools{ private LoginPage loginPage = new LoginPage();private UserHomePage userHomePage = new UserHomePage();public LoginPageTools AssertUserName(string userName){UserHomePage.AssertUserName(userName);return this; } }public LoginPageTools AssertLoginFailed(){loginPage.IsLoginFailed();return this;}public LoginPageTools PleaseLogin(string userName, string password){BaseObject.InitPage(loginPage);loginPage.EnterUserName(userName).EnterPassword(password).ClickLoginButton();return this;} } } 16. HomePagepublic const string DISPLAYED_USER_NAME ="//img[contains(@id,profile_pic_header)]/../span[contains(text(),#)]";public const string USER_NAVIGATION_MENU = "//*[@id=userNavigationLabel]";public const string LOGOUT_BUTTON = "//input[@value=Log Out]";[FindsBy(How = How.XPath, Using = USER_NAVIGATION_MENU)]public IWebElement userNavigationMenu;[FindsBy(How = How.XPath, Using = LOGOUT_BUTTON)]public IWebElement logoutButton;public static UserHomePage GetUserHomePage(){UserHomePage userHomePage = new UserHomePage();InitPage(userHomePage);return userHomePage;}public UserHomePage AssertUserName(string testUserName){TestTools.WaitingForElement(DISPLAYED_USER_NAME.Replace("#",testUserName));return GetUserHomePage();} 17. HomePagenamespace FaceBookTest{class UserHomePageTools{private UserHomePage userHomePage = new UserHomePage();public UserHomePageTools PleaseLogOut(){BaseObject.InitPage(userHomePage);userHomePage.userNavigationMenu.Click();userHomePage.logoutButton.Click();return this;}}} 18. namespace FaceBookTest{ public class LoginPageTest : BaseTest{private static LoginPageTools loginPageTools = new LoginPageTools();private static UserHomePageTools userHomePageTools = newUserHomePageTools();[Test]public static void UserIsAbbleToLogin(){loginPageTools.PleaseLogin(fbuser@mail.com, fb!password13).AssertUserName(FBtest User);UserHomePageTools.PleaseLogOut();}[Test]public static void UserIsNotAbbleToLogin(){PleaseLogin(fbuser@mail.com, incorrectPassword).AssertLoginFailed();}}} 19. 20. SpecFlow : SpecFlow SpecFlow.NUnit SpecFlow Feature File 21. namespace FaceBookTest{ [Binding]public class LoginFeatureSteps : BaseTest{ private static LoginPageTools loginPageTools = new LoginPageTools();private static UserHomePageTools userHomePageTools = newUserHomePageTools();[AfterScenario()]public void TearDown(){ afterClass(); }[Given(@"FaceBook Login Page is opened")]public void GivenFaceBookLoginPageIsOpened(){ specFlowInit();}[When(@"Enter credentials (.*)/(.*) and press Login button")]public void WhenEnterCredentialsAndPressLogin(string p0, string p1){ loginPageTools.PleaseLogin(p0, p1); }[Then(@"User Name (.*) is displayed on the Home Page")]public void ThenUserNameIsDisplayedOnTheHomePage(string p0){ loginPageTools.AssertUserName(p0);userHomePageTools.PleaseLogOut(); }} } BaseTest.spublic void specFlowInit(){beforeClass();beforeTest();} 22. //