1

Im running a project with Selenium where Im trying to automate some web navigation. The project consists of 2 maven projects, where one of the project is responsible for setting up the Firefox drivers and to find the title element of the main page of a site as well as the title element of a certain "path" of the site. The other maven project has the first project as a dependency to get all the jar files in order to execute Junit tests.

I have been successful in fetching the title of the main page but for some reason I cant do it for the second (path) page when executing my Junit tests. I have been getting several kinds of messages like "ElementNotInteractable Exception", "NoSuchElement Exception", "StaleElement" etc.

Because of this I thought I might solve it with a ExplicitWait in order for the element to be clickable but for no good. Despite that I close the first popup when the site opens for the first time (A Cookie accept page... we have all seen those!) Firefox wont be able to fetch the title of the second page (subpage/path) when I use Xpath expression.

I just feel so lost now, dont know why the browser wants to keep arguing with me despite that I have made several attempts to make it work, like making an ImplicitWait, using a WebdriverWait to wait for the element to be visible, etc.

To make it more clear instead of me just writing, I have attached my project for you so you could take a look at it and maybe be able spot the issue Im facing. Maybe you are able run it successfully in your Firefox browser? (Btw I have the latest Firefox version and the latest driver too)

Classes and the code for the first Maven project (Setup of drivers, navigation)

package com.pluralsight;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Browser {

    static WebDriver driver = new FirefoxDriver();
    

    public static void goTo(String url) {
        driver.get(url);
    }

    public static String title() {
        return driver.getTitle();
    }

    public static void close() {
        driver.close();
    }

}




package com.pluralsight;

public class Pages {

    public static HomePage homePage() {
        return new HomePage();
    }

    public static PathPages pathPages() {
        PathPages pathPages = new PathPages();
        return pathPages;
    }

}


package com.pluralsight;

public class HomePage {

    static String url = "https://www.blocket.se";
    static String title = "Blocket - köp & sälj bilar, möbler, lägenheter, cyklar och mer";

    public void goTo() {
        Browser.goTo(url);

    }
    
    
    public boolean gettingCorrectTitle() {
        return Browser.title().equals(title);

    }

}


*** This is the place where the problems start ***

package com.pluralsight;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class PathPages {

    static String url = "http://www.blocket.se/annonser/hela_sverige/fordon?cg=1000";
    static String title = "Fordon säljes i Skåne - Blocket";


    
    public void goTo() {
        Browser.goTo(url);
    }

    public void goToVehiclePath(){
        WebElement element = Browser.driver.findElement(By.xpath("/html/head/title"));
        WebDriverWait wait = new WebDriverWait(Browser.driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(element));
        element.click();
    }

    public boolean gettingCorrectTitle() {
        return Browser.title().equals(title);
    }

}

Junit testclass for the second Maven project

package com.pluralsight;

import org.junit.AfterClass;
import org.junit.Test;

import junit.framework.Assert;

@SuppressWarnings("deprecation")
public class UnitTest {

    @Test
    public void canGoToHomePage() {
        Pages.homePage().goTo();
        Assert.assertTrue(Pages.homePage().gettingCorrectTitle());
    }

    
    
    @Test
    public void canGoToVehiclePathPage() {
        Pages.pathPages().goTo();
        Pages.pathPages().goToVehiclePath();
        Assert.assertTrue(Pages.pathPages().gettingCorrectTitle());
    }
    
    
    @AfterClass
    public static void cleanUp() {
        Browser.close();
    }

}

Sorry if the post got a bit long...

Extremely grateful for every tips and suggestion that can help me in getting in the right direction :)

Erfan Tavoosi
  • 389
  • 5
  • 16

0 Answers0