2

My problem:

My question:

How to check content of this txt file opened in new Firefox window?

1
  • 1
    Assert that Driver.FindElement(ByCssSelector="pre").Text.Contains("what you want") or (would be better) download file and use programming language methods to read file content Commented Jan 18, 2015 at 5:50

2 Answers 2

2

Because you're opening a new tab in your browser, you can follow this pattern.

  1. Once the tab is opened, make Selenium switch its focus to it. If you don't know how, this question has got several answers here or elsewhere.
  2. Apparently, Firefox puts the file content in a <pre> tag so you have a valid HTML document. So it's easy to use driver.findElement to get the contents in the tag.
  3. You now have the whole file's contents to do the string operations you want.
1

When you open a text(.txt) file in a browser and check the structure of the contents in the console, you will notice that it's HTML and the content of the file are enclosed within <pre></pre> tag.

So what you can do is switch to the new tab (since you are opening the file in new tab) and then user getText() to fetch the content of the <pre></pre> tag.

Here is a sample code I tried and it worked:

public static void main(String[] args) {
        try {
            WebDriver driver;
            //System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
            //driver = new ChromeDriver();
            System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "//geckodriver");
            driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.get("http://localhost/1/test.html");
            driver.manage().window().maximize();
            Thread.sleep(2000);

            String s = null;
            driver.findElement(By.tagName("a")).click();
            Thread.sleep(2000);
            ArrayList tabs = new ArrayList (driver.getWindowHandles());
            driver.switchTo().window((String) tabs.get(1));
            String filecontent = driver.findElement(By.tagName("pre")).getText();
            System.out.println(filecontent);

            Thread.sleep(5000);
            driver.close();
            driver.quit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.