简体   繁体   中英

I need assistance in creating a function that will read (open new browser tab)url from CSV file in Java (Selenium)

I am trying to learn java and selenium by myself and creating a robot that will scan job/career pages for certain string (job name eg QA , developer...)

I'm trying to create JAVA code using selenium, that will read URL links from CSV file and open a new tab.

the main goal is to add several url in the CSV and assert/locate a certain string in the designated url's for example: is there "Careers" link in each URL, the test will pass for this specific url.

  1. created a selenium project
  2. created new chromeDriver
  3. Created CSV built from 3 columns (ID, company's name, URL) - and added it to the project
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class URLSearch {
    public static void main(String[] args) {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        String fileName = "JobURLList.csv";
        File file = new File(fileName);  //read from file

        try {
            Scanner inputStream = new Scanner(file);
            while (inputStream.hasNext()) {
                String data = inputStream.next();
                System.out.println(data);
            }
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
  1. first line in the CSV - titles: id, name, url
  2. Read the url from the second line - eg https://careers.google.com/jobs/ "
  3. open browsertab and start going over the url list (from the CSV)
  4. locate a hardcoded string (eg "developer" , "qa" ..) in each url
  5. if such a string was found, write in console the url that the test turned out to be positive (such a string was found in one of the url's).
  6. if no such string was found, skip to the next url.

To open the new tab do something like this (this assumes "driver" object is your WebDriver):

  ((JavascriptExecutor)driver).executeScript("window.open('about:blank', '_blank');"); 

        Set<String> tab_handles = driver.getWindowHandles();
        int number_of_tabs = tab_handles.size();
        int new_tab_index = number_of_tabs-1;
        driver.switchTo().window(tab_handles.toArray()[new_tab_index].toString());

You could then create a function that takes a list of key/value pairs, with URL and term to search for and loop through it. Do you want to use a hashmap for this, or maybe an ArrayList of a class (id/name/url)? The code for finding the text would be something like this (assumes you've defined a var of "Pass" to boolean):

   driver.get([var for URL]);
   //driver will wait for pageready state, so you may
   // not need the webdriver wait used below.  Depends
   // on if the page populates data after pagereadystate
      String xpather = "//*[contains(text(), '" + [string var for text to search for] + "')]";
        try
            {
        wait = new WebDriverWait(driver, 10);
            List<WebElement> element =  wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(xpather)));   
        this.Pass = false;
        if (element.size() > 0)
        {
            this.Pass = true;

        }

            }
            catch (Exception ex)
            {
                this.Pass = false;
                System.out.println ("Exception finding text: " + ex.toString());
            }

Then logic for if (this.Pass==true or false)..

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