FILE HANDLING SCENARIO’S IN SELENIUM WEBDRIVER:
1. Read the text displayed in the WEBSITE: Actual
Automate Coronavirus COVID19 REST API POST METHOD Tutorial Part2
2. Maintain this text in a txt file : Expected
Automate Coronavirus COVID19 REST API POST METHOD Tutorial Part2
3. Compare the values from STEP 1 and STEP 2.
4. Mark the TESTCASE as PASS/ FAIL.
JAVA API FOR READING THE CONTENTS OF THE TEXT FILE:
Following are the java classes useful in reading the text from the file.
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
Java Logic
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 |
package seleniumexamples; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class FileOperations { @Test public void validateTextfromWebPage() { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("http://total-qa.com"); String actual = driver.findElement(By.xpath("//h2[@class='entry-title']/a")).getText(); String expected = readFileContents(); Assert.assertEquals(actual, expected); } public String readFileContents() { //Java Logic for reading the file WebsiteContent.txt File f = new File("WebsiteContent.txt"); FileReader fr = null; BufferedReader br=null; String text=""; try { fr = new FileReader(f.getAbsolutePath()); br = new BufferedReader(fr); String str=""; while((str=br.readLine())!=null) { text = text+str; System.out.println("File Contents:: " + str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { fr.close(); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return text; } } |
Conclusion:
File Contents:: Automate Coronavirus COVID19 REST API POST METHOD Tutorial Part2
PASSED: validateTextfromWebPage