|
2 | 2 |
|
3 | 3 | import java.io.File;
|
4 | 4 | import java.time.Duration;
|
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
5 | 7 | import java.util.logging.Level;
|
6 | 8 |
|
| 9 | +import org.openqa.selenium.By; |
| 10 | +import org.openqa.selenium.JavascriptExecutor; |
7 | 11 | import org.openqa.selenium.WebDriver;
|
| 12 | +import org.openqa.selenium.WebElement; |
8 | 13 | import org.openqa.selenium.chrome.ChromeDriver;
|
9 | 14 | import org.openqa.selenium.chrome.ChromeDriverService;
|
10 | 15 | import org.openqa.selenium.chrome.ChromeOptions;
|
@@ -46,10 +51,174 @@ public void endTest() {
|
46 | 51 | driver.quit();
|
47 | 52 | }
|
48 | 53 |
|
49 |
| - public void testCase01() throws InterruptedException { |
| 54 | + /** <STRONG>Test Case 01</STRONG>: Verify the Leetcode Homepage URL <p> |
| 55 | + * <STRONG>Description</STRONG>: Verify that the Leetcode homepage URL contains "leetcode" <p> |
| 56 | + * <STRONG>Expected Result</STRONG>: The URL of the Leetcode homepage contains "leetcode" <p> |
| 57 | + * |
| 58 | + * 1. Launch chrome browser <p> |
| 59 | + * 2. Navigate to the Leetcode website https://leetcode.com <p> |
| 60 | + * 3. Verify that the URL contains "leetcode" <p> |
| 61 | + */ |
| 62 | + public void testCase01() { |
50 | 63 | System.out.println("\nTestCase01: START");
|
51 |
| - driver.get("https://www.google.com"); |
52 |
| - Thread.sleep(5000); |
| 64 | + |
| 65 | + // Launch chrome browser |
| 66 | + // Navigate to the Leetcode website https://leetcode.com |
| 67 | + driver.get("https://leetcode.com"); |
| 68 | + |
| 69 | + // Verify that the URL contains "leetcode" |
| 70 | + String actualUrl = driver.getCurrentUrl(); |
| 71 | + String expectedUrlContent = "leetcode"; |
| 72 | + |
| 73 | + if (actualUrl.contains(expectedUrlContent)) { |
| 74 | + System.out.println("TestCase01: The URL contains 'leetcode': PASS"); |
| 75 | + } else { |
| 76 | + System.out.println("TestCase01: The URL does not contains 'leetcode': FAIL"); |
| 77 | + } |
| 78 | + |
53 | 79 | System.out.println("TestCase01: END\n");
|
54 | 80 | }
|
| 81 | + |
| 82 | + /** <STRONG>Test Case 02</STRONG>: Verify Problem Set URL and Display First 5 Questions <p> |
| 83 | + * <STRONG>Description</STRONG>: Retrieve details of the first 5 questions on the problems page <p> |
| 84 | + * <STRONG>Expected Result</STRONG>: The correct details of the problems are obtained and verified <p> |
| 85 | + * |
| 86 | + * 1. Launch chrome browser <p> |
| 87 | + * 2. Navigate to the Leetcode website https://leetcode.com <p> |
| 88 | + * 3. Scroll to view question link <p> |
| 89 | + * 4. Click on the "View Questions" link on the Leetcode homepage <p> |
| 90 | + * 5. Verify that you are on the problem set page, by checking the URL contains "problemset" <p> |
| 91 | + * 6. Retrieve the details of the first 5 questions and print them <p> |
| 92 | + * 7. Make sure to check that the title of each question is correct and that you are selecting the questions from the first problem, i.e., "Two Sum" <p> |
| 93 | + */ |
| 94 | + public void testCase02() { |
| 95 | + System.out.println("\nTestCase02: START"); |
| 96 | + |
| 97 | + // Launch chrome browser |
| 98 | + // Navigate to the Leetcode website https://leetcode.com |
| 99 | + driver.get("https://leetcode.com"); |
| 100 | + |
| 101 | + // Scroll to view question link |
| 102 | + WebElement viewQuestionLink = driver.findElement(By.xpath("//p[contains(text(), 'View Questions')]")); |
| 103 | + ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", viewQuestionLink); |
| 104 | + |
| 105 | + // Click on the "View Questions" link on the Leetcode homepage |
| 106 | + viewQuestionLink.click(); |
| 107 | + |
| 108 | + // Verify that you are on the problem set page, by checking the URL contains "problemset" |
| 109 | + String currentUrl = driver.getCurrentUrl(); |
| 110 | + String expectedUrlContains = "problemset"; |
| 111 | + if (currentUrl.contains(expectedUrlContains)) { |
| 112 | + System.out.println("TestCase02: The URL contains 'problemset': PASS"); |
| 113 | + } else { |
| 114 | + System.out.println("TestCase02: The URL does not contains 'problemset': FAIL"); |
| 115 | + } |
| 116 | + |
| 117 | + // Retrieve the details of the first 5 questions and print them |
| 118 | + WebElement questionList = driver.findElement(By.xpath("(//div[contains(@role, 'rowgroup')])[3]")); |
| 119 | + ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", questionList); |
| 120 | + |
| 121 | + List<WebElement> questionsTitle = driver.findElements(By.xpath("(//div[contains(@class, 'truncate')]/a)[position()>1 and position() <= 6]")); |
| 122 | + System.out.println("First 5 questions are:"); |
| 123 | + for (WebElement question : questionsTitle) { |
| 124 | + System.out.println("\t\t" + question.getText()); |
| 125 | + } |
| 126 | + |
| 127 | + // Make sure to check that the title of each question is correct and that you are selecting the questions from the first problem, i.e., "Two Sum" |
| 128 | + List<String> expectedQuestions = Arrays.asList("Two Sum", "Add Two Numbers", "Longest Substring Without Repeating Characters", "Median of Two Sorted Arrays", "Longest Palindromic Substring"); |
| 129 | + System.out.println("Check that the title of each question is correct:"); |
| 130 | + for (int i=0; i<5; i++) { |
| 131 | + boolean isCorrectTitle = questionsTitle.get(i).getText().contains(expectedQuestions.get(i)); |
| 132 | + System.out.println("\t\t" + questionsTitle.get(i).getText() + ": " + (isCorrectTitle ? "Correct title" : "Incorrect title")); |
| 133 | + } |
| 134 | + |
| 135 | + System.out.println("TestCase02: END\n"); |
| 136 | + } |
| 137 | + |
| 138 | + /** <STRONG>Test Case 03</STRONG>: Verify the Two Sum problem <p> |
| 139 | + * <STRONG>Description</STRONG>: Open the Two Sum problem and verify the URL <p> |
| 140 | + * <STRONG>Expected Result</STRONG>: The URL of the problem contains "two-sum" <p> |
| 141 | + * |
| 142 | + * 1. Launch chrome browser <p> |
| 143 | + * 2. Navigate to the Leetcode website https://leetcode.com <p> |
| 144 | + * 3. Scroll to view question link <p> |
| 145 | + * 4. Click on the "View Questions" link on the Leetcode homepage <p> |
| 146 | + * 5. Open the first problem i.e, Two Sum <p> |
| 147 | + * 6. Verify that the URL contains "two-sum" <p> |
| 148 | + */ |
| 149 | + public void testCase03() { |
| 150 | + System.out.println("\nTestCase03: START"); |
| 151 | + // Launch chrome browser |
| 152 | + // Navigate to the Leetcode website https://leetcode.com |
| 153 | + driver.get("https://leetcode.com"); |
| 154 | + |
| 155 | + // Scroll to view question link |
| 156 | + WebElement viewQuestionLink = driver.findElement(By.xpath("//p[contains(text(), 'View Questions')]")); |
| 157 | + ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", viewQuestionLink); |
| 158 | + |
| 159 | + // Click on the "View Questions" link on the Leetcode homepage |
| 160 | + viewQuestionLink.click(); |
| 161 | + |
| 162 | + // Open the first problem i.e, Two Sum |
| 163 | + WebElement twoSum = driver.findElement(By.xpath("//a[contains(normalize-space(),'Two Sum')]")); |
| 164 | + ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", twoSum); |
| 165 | + twoSum.click(); |
| 166 | + |
| 167 | + // Verify that the URL contains "two-sum" |
| 168 | + String actualUrl = driver.getCurrentUrl(); |
| 169 | + String expectedUrlContent = "two-sum"; |
| 170 | + |
| 171 | + if (actualUrl.contains(expectedUrlContent)) { |
| 172 | + System.out.println("TestCase03: The URL contains 'two-sum': PASS"); |
| 173 | + } else { |
| 174 | + System.out.println("TestCase03: The URL does not contains 'two-sum': FAIL"); |
| 175 | + } |
| 176 | + |
| 177 | + System.out.println("TestCase03: END\n"); |
| 178 | + } |
| 179 | + |
| 180 | + /** <STRONG>Test Case 04</STRONG>: Verify the submissions tab displays "Register or Sign In" <p> |
| 181 | + * <STRONG>Description</STRONG>: Check the submissions tab and verify if it displays "Register or Sign In" <p> |
| 182 | + * <STRONG>Expected Result</STRONG>: The message "Register or Sign In" is displayed when you click on the submissions tab. <p> |
| 183 | + * |
| 184 | + * 1. Launch chrome browser <p> |
| 185 | + * 2. Navigate to the Leetcode website https://leetcode.com <p> |
| 186 | + * 3. Scroll to view question link <p> |
| 187 | + * 4. Click on the submission tab <p> |
| 188 | + * 5. Verify that it displays "Register or Sign In" <p> |
| 189 | + */ |
| 190 | + public void testCase04() { |
| 191 | + System.out.println("\nTestCase04: START"); |
| 192 | + // Launch chrome browser |
| 193 | + // Navigate to the Leetcode website https://leetcode.com |
| 194 | + driver.get("https://leetcode.com"); |
| 195 | + |
| 196 | + // Scroll to view question link |
| 197 | + WebElement viewQuestionLink = driver.findElement(By.xpath("//p[contains(text(), 'View Questions')]")); |
| 198 | + ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", viewQuestionLink); |
| 199 | + |
| 200 | + // Click on the "View Questions" link on the Leetcode homepage |
| 201 | + viewQuestionLink.click(); |
| 202 | + |
| 203 | + // Open the first problem i.e, Two Sum |
| 204 | + WebElement twoSum = driver.findElement(By.xpath("//a[contains(normalize-space(), 'Two Sum')]")); |
| 205 | + ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", twoSum); |
| 206 | + twoSum.click(); |
| 207 | + |
| 208 | + // Click on the submission tab |
| 209 | + WebElement submissionTab = driver.findElement(By.xpath("(//div[contains(text(), 'Submission')])[2]")); |
| 210 | + submissionTab.click(); |
| 211 | + |
| 212 | + // Verify that it displays "Register or Sign In" |
| 213 | + String status = "FAIL"; |
| 214 | + try { |
| 215 | + WebElement registerOrSignIn = driver.findElement(By.xpath("//a[contains(text(), 'Register or Sign In')]")); |
| 216 | + if (registerOrSignIn.getText().equals("Register or Sign In")) { |
| 217 | + status = "PASS"; |
| 218 | + } |
| 219 | + } catch (Exception e) {} |
| 220 | + System.out.println("TestCase04: Submission tab displays 'Register or Sign In': " + status); |
| 221 | + |
| 222 | + System.out.println("TestCase04: END\n"); |
| 223 | + } |
55 | 224 | }
|
0 commit comments