简体   繁体   中英

Selenium webdriver load links from external file

I've been creating some tests in Selenium IDE, but I've now decided to move onto Selenium Webdriver and Java. I'm looking at creating a smoke test using java and Selenium Webdriver, which would navigate througb a site and verify the page was displayed successfully. Ideally I would want to be able to use the smoke test on different sites.

To do this my initial thinking was to keep the locator of the links, buttons etc in an external file and identify what method to use (eg click, select etc.). The code would use the information from the file to then navigate through the site and report whetber the page was successfuly displayed or not.

I managed to do a similar approach in Selenium IDE and created a basic version using a few addons, but I was fairly limited in the reporting and error handling. I did this by having an external file, which included the locator and a number to identify the command. These were stored in variables and I used an if statement to pick whether a clickAndWait, click or select command was required.

I was wondering if someone could tell me if this could be done using Java and Selenium Webdriver? And if it can be done some guidance how?

In java you read inputs from external files from .txt/.JSON formates Here I am giving example with .txt file

input1.txt ('http://' before website url in selenium)

http://www.stackoverflow.com
http://www.website1.com 

Code

public class test1  {
public static void main(String[] args) throws InterruptedException,IOException  {
String s1 = new String(Files.readAllBytes(Paths.get("C:\\input1.txt")));
String[] URL = s.split(System.getProperty("line.separator"));
WebDriver driver = new FirefoxDriver();


for(int i=0;i<URL.length;i++){
     driver.get(URL[i]);
     driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

try {  \\Your own validation logic. 
    driver.getPageSource().contains(" <Some KEY_WORD> ")
    System.out.println(URL[i]+" is a valid");
     }

 catch (Exception e) {
     System.out.println(URL[i]+" is a not-valid url");
  }
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM