This is a multi part series of videos and tutorials. Most of these sessions are recorded during the live class room sessions.
PART 1: Basic Selenium Test using public static void main
PART 2: Basic Test using TestNG
PART 3: Test NG , Maven, Selenium first time Use
Part 3 Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package parabank_automation.product.tc; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; import org.testng.Assert; import org.testng.Reporter; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; public class TC_Login { WebDriver driver=null; String url = "http://parabank.parasoft.com"; @BeforeSuite public void BeforeSuite1() { //Configurations- chromedriver path setting System.setProperty("webdriver.chrome.driver", "/Users/akashtyagi/Desktop/chromedriver"); } @BeforeMethod public void BeforeMethod1() { driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().window().maximize(); Reporter.log("Chrome Driver Invoked and maximized", true); } @AfterMethod public void Aftermethod1() { driver.quit(); Reporter.log("Browser quit",true); } @Test public void t_01_validate_url_is_working() { // 2. Navigate to URL driver.get(url); String actual = driver.getTitle(); String expected = "ParaBank | Welcome | Online Banking"; Assert.assertEquals(actual, expected); Reporter.log("Url navigated: " + url,true); } @Test public void t_02_validate_parabank_login() { // 2. Navigate to URL driver.get(url); Reporter.log("Url navigated: " + url,true); // 3. Enter User Name WebElement oUserName = driver.findElement(By.name("username")); oUserName.sendKeys("john"); Reporter.log("Username entered as john",true); // 4. Enter Password WebElement oPassword = driver.findElement(By.name("password")); oPassword.sendKeys("demo"); Reporter.log("Password entered as demo",true); // 5. Submit-XPATH WebElement oSubmit = driver.findElement(By.xpath("//*[@id='loginPanel']/form/div[3]/input")); oSubmit.click(); Reporter.log("Submit Button Clicked.",true); // 6. Validation-Title String expected = "ParaBank | Accounts Overview"; String actual = driver.getTitle(); //Assert.assertEquals(actual, expected); if (expected.equals(actual)) { Assert.assertTrue(true); Reporter.log("Page Title correctly Displayed.",true); }else { Reporter.log("Page Title not Displayed.",true); Assert.assertTrue(false); } } } |
PART 4: Page Object Model, Page Factory
Test Case file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
package parabank_automation.product.tc; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.PageFactory; import org.testng.annotations.Test; import parabank_automation.product.po.PO_Cmn; import parabank_automation.product.po.PO_Login; import parabank_automation.product.po.PO_TransferFunds; import org.testng.Assert; import org.testng.Reporter; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeClass; public class TC_Login { WebDriver driver=null; String url = "http://parabank.parasoft.com"; @BeforeSuite public void BeforeSuite1() { //Configurations- chromedriver path setting System.setProperty("webdriver.chrome.driver", "/Users/akashtyagi/Desktop/chromedriver"); } @BeforeClass public void BeforeClass() { driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().window().maximize(); Reporter.log("Chrome Driver Invoked and maximized", true); } @AfterClass public void AfterClass() { driver.quit(); Reporter.log("Browser quit",true); } @Test(priority=1) public void t_01_validate_url_is_working() { // 2. Navigate to URL driver.get(url); String actual = driver.getTitle(); String expected = "ParaBank | Welcome | Online Banking"; Assert.assertEquals(actual, expected); Reporter.log("Url navigated: " + url,true); } @Test(priority=2,dependsOnMethods = "t_01_validate_url_is_working") public void t_02_validate_parabank_login() { //Initialize the Page object PO_Login obj = PageFactory.initElements(driver, PO_Login.class); obj.EnterUserName("john"); obj.EnterPassword("demo"); obj.ClickSubmitButton(); // 6. Validation-Title String expected = "ParaBank | Accounts Overview"; String actual = driver.getTitle(); //Assert.assertEquals(actual, expected); if (expected.equals(actual)) { Assert.assertTrue(true); Reporter.log("Page Title correctly Displayed.",true); }else { Reporter.log("Page Title not Displayed.",true); Assert.assertTrue(false); } } @Test(priority=3,dependsOnMethods = "t_02_validate_parabank_login") public void t_03_transfer_funds() { String fromAccount = "12456"; String toAccount = "12900"; String amount = "100"; //Transfer Funds Clicked. PO_Cmn oPO_Cmn = PageFactory.initElements(driver, PO_Cmn.class); oPO_Cmn.ClickOnTranferFunds(); //Tranfer Funds Operation PO_TransferFunds oTransferFunds = PageFactory.initElements(driver, PO_TransferFunds.class); oTransferFunds.EnterTextInAmount(amount); oTransferFunds.SelectAccountFrom(fromAccount); oTransferFunds.SelectAccountTo(toAccount); oTransferFunds.ClickOnTranferFundsButton(); //Validation oTransferFunds.ValidateTransferFundsIsSuccessfull(fromAccount, toAccount, amount); } /* @Test public void t_02_validate_parabank_login() { // 2. Navigate to URL driver.get(url); Reporter.log("Url navigated: " + url,true); // 3. Enter User Name WebElement oUserName = driver.findElement(By.name("username")); oUserName.sendKeys("john"); Reporter.log("Username entered as john",true); // 4. Enter Password WebElement oPassword = driver.findElement(By.name("password")); oPassword.sendKeys("demo"); Reporter.log("Password entered as demo",true); // 5. Submit-XPATH WebElement oSubmit = driver.findElement(By.xpath("//*[@id='loginPanel']/form/div[3]/input")); oSubmit.click(); Reporter.log("Submit Button Clicked.",true); // 6. Validation-Title String expected = "ParaBank | Accounts Overview"; String actual = driver.getTitle(); //Assert.assertEquals(actual, expected); if (expected.equals(actual)) { Assert.assertTrue(true); Reporter.log("Page Title correctly Displayed.",true); }else { Reporter.log("Page Title not Displayed.",true); Assert.assertTrue(false); } } */ } |
Page Object Model File for Transfer Funds:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package parabank_automation.product.po; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; import org.openqa.selenium.support.ui.Select; import org.testng.Assert; public class PO_TransferFunds { //1st Section WebDriver driver; //2nd Paramatrized constructor public PO_TransferFunds(WebDriver driver) { this.driver = driver; } //3rd Section: Locators @FindBy(how = How.ID,using = "amount") private WebElement txtbx_amount; @FindBy(how = How.ID,using = "fromAccountId") private WebElement drop_down_from_account; @FindBy(how = How.ID,using = "toAccountId") private WebElement drop_down_to_account; @FindBy(how = How.XPATH,using = "//input[@value='Transfer']") private WebElement btn_submit; @FindBy(how = How.XPATH,using = "//div[@ng-if='showResult']") private WebElement div_text_transfer_success_message; public void EnterTextInAmount(String text) { txtbx_amount.sendKeys(text); } public void SelectAccountFrom(String text) { Select list = new Select(drop_down_from_account); list.selectByVisibleText(text); } public void SelectAccountTo(String text) { Select list = new Select(drop_down_to_account); list.selectByVisibleText(text); } public void ClickOnTranferFundsButton() { btn_submit.click(); } public void ValidateTransferFundsIsSuccessfull(String fromAccount, String toAccount, String amount) { String actual_message_content = div_text_transfer_success_message.getText(); //Transfer complete message if (actual_message_content.equals("Transfer Complete!")) { Assert.assertTrue(true,"Transfer Funds Message is coming"); }else { Assert.assertTrue(false,"Transfer Funds Message is not coming"); } //From Account if (actual_message_content.equals(fromAccount)) { Assert.assertTrue(true,"From Account is correctly displayed"); }else { Assert.assertTrue(false,"From Account is not correctly displayed"); } //From Account if (actual_message_content.equals(toAccount)) { Assert.assertTrue(true,"To Account is correctly displayed"); }else { Assert.assertTrue(false,"To Account is not correctly displayed"); } //AMount if (actual_message_content.equals(amount)) { Assert.assertTrue(true,"Amount is correctly displayed."); }else { Assert.assertTrue(false,"Amount is not correctly displayed."); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class PO_Cmn { //1st Section WebDriver driver; //2nd Paramatrized constructor public PO_Cmn(WebDriver driver) { this.driver = driver; } //3rd Section: Locators @FindBy(how = How.LINK_TEXT,using = "Transfer Funds") private WebElement link_transfer_funds; public void ClickOnTranferFunds() { link_transfer_funds.click(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public class PO_Login { //1st Section WebDriver driver; //2nd Paramatrized constructor public PO_Login(WebDriver driver) { this.driver = driver; } //3rd Section: Locators @FindBy(how = How.NAME,using = "username") private WebElement txtbx_username; @FindBy(how = How.NAME,using = "password") private WebElement txtbx_password; @FindBy(how = How.XPATH,using = "//*[@id='loginPanel']/form/div[3]/input") private WebElement btn_submit; //4th Section- methods public void EnterUserName(String text) { txtbx_username.sendKeys(text); } public void EnterPassword(String text) { txtbx_password.sendKeys(text); } public void ClickSubmitButton() { btn_submit.click(); } |
Part 5: TO be ADDED HERE