This code is a Sample code to describe how tests can be created using Junit and best way to structure them. This is just to explain Junit functioning and how things can be managed in a class and make things reusable.
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 |
/** * */ package login.testcases; import static org.junit.jupiter.api.Assertions.*; import java.util.List; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; /** * @author Akash Tyagi * date: 5Aug2018 */ class Login { WebDriver driver; WebElement txt_username; WebElement txt_password; WebElement btn_submit; /* * This method will be called once per Class * So chrome driver path set up is done here */ @BeforeAll static void setUpBeforeClass() throws Exception { System.setProperty("webdriver.chrome.driver", "D:\\VisionITWorkspace\\dependencies\\chromedriver_win32\\chromedriver.exe"); } /* * This method will be called after end of all tests * and last Before each */ @AfterAll static void tearDownAfterClass() throws Exception { //Nothing to Add here for now } /* * This method is called before each test * So Driver intialization and URL navigation * is done here, since these steps will be required * for each test case. And its a good practice to * make your test case independant of each other. */ @BeforeEach void setUp() throws Exception { //1 Create Driver object driver = new ChromeDriver(); //Navigate driver.get("http://parabank.parasoft.com"); } /* * Quiting the browser */ @AfterEach void tearDown(){ //Clean Up driver.close(); } /* * Test 1. Sequence is not defined */ @Test void t_validate_login_in_to_parabank() { //This method will initialize login page Locators f_init_login_locators(); //Operation on- Login in to application txt_username.sendKeys("john"); txt_password.sendKeys("demo"); btn_submit.click(); //Need to Add validation Point }//end method /* * Test 2. Sequence is not defined */ @Test void t_validate_negative_enter_invalid_credentials() { //Initialize Locators f_init_login_locators(); //Operation on- Login in to application txt_username.sendKeys("Akash"); txt_password.sendKeys("ABCDPASSS"); btn_submit.click(); //Need to Add validation Point } /* * Reusable code to to store the locators at one location * Later in its place we can use Page Object Model Class */ public void f_init_login_locators() { //Locators for Login txt_username = driver.findElement(By.xpath("//input[@name='username' and @class='input']")); txt_password = driver.findElement(By.xpath("//input[@name='password' and @class='input']")); btn_submit = driver.findElement(By.xpath("//input[@value='Log In' and @type='submit']")); } }//end class |