Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
221 views
by The go-to Tester (218 points)
I have written selenium webdriver test and want o test back and forward options of browser. My code is

@Test
    public void testBrowserHistory() throws Exception
    {
        //Load web page
        d.navigate().to("http://docs.seleniumhq.org/");
        assertEquals("Selenium - Web Browser Automation",d.getTitle());
        //Click on Download Tab
        d.findElement(By.linkText("Download")).click();
        System.out.println(d.getCurrentUrl());
        d.navigate().back();
        System.out.println(d.getCurrentUrl());
        d.navigate().forward();
        System.out.println(d.getCurrentUrl());
        d.navigate().refresh();
        System.out.println(d.getCurrentUrl());
    }
    @Before
    public void setUp()
    {
        //Launch browser
        System.setProperty("webdriver.gecko.driver", "C://Selenium driver/geckodriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        d=new FirefoxDriver();
        d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        d.manage().deleteAllCookies();
    }
    @After
    public void tearDown()
    {
        //Close browser
        d.quit();
    }

}

The output is

http://docs.seleniumhq.org/
http://docs.seleniumhq.org/
http://docs.seleniumhq.org/
http://docs.seleniumhq.org/

The browser does not go to download page. What is the problem.

1 Answer

0 like 0 dislike
by

After clicking on Download button, wait for Download page to appear. 

You can wait for title of the Download page to make sure that Download page is loaded.

WebDriverWait wait = new WebDriverWait(d, 15);
wait.until(ExpectedConditions.titleIs("Downloads"));

Then do 

System.out.println(d.getCurrentUrl());
 

Also, see if 

d.findElement(By.linkText("Download")).click();

is working. If not, you can try using xpath: 

//*[@id="menu_download"]/a


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...