I have a test case that goes something like this:
- open homepage
- if there is no content present
- refresh page
- continue with other steps...
This is the relevant part of the code:
public JpoPO() {
driver.get(Settings.JPO_TEST_URL);
PageFactory.initElements(driver, this);
System.out.println("[INFO] Homepage initialized.");
zatvoriModal();
refreshIfNeeded();
zatvoriModal();
(new WebDriverWait(driver, 30)).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#loading")));
System.out.println("[DEBUG] broj .ng-scope elemenata: " +driver.findElements(By.cssSelector(".ng-scope")).size());
System.out.println("[OK] JpoPO() initialized.");
}
And this is the refreshIfNeeded()
part:
public void refreshIfNeeded() {
if(System.getProperty("os.name").equals("Linux")){
System.out.println("### A"+now());
int broj = driver.findElements(By.cssSelector(".ng-scope")).size();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("location.reload()");
//driver.findElement(By.cssSelector("header")).sendKeys(Keys.F5);
driver.get(driver.getCurrentUrl());
waitForNoSpinner();
System.out.println("[DEBUG] location reloaded, .ng-scope elements: "+broj);
System.out.println("### B"+now());
}else{
System.out.println("[] Starting refreshIfNeeded()");
Date ts1 = new Date();
int count = 0;
while (driver.findElements(By.cssSelector(".ng-scope")).size()==0 && count < 10){
driver.navigate().refresh();
zatvoriModal();
try {
(new WebDriverWait(driver, 10)).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector(".ng-scope"), 0));
Date ts2 = new Date();
long trajanje = ts2.getTime() - ts1.getTime();
System.out.println(String.format("[INFO] Učitavanje sadržaja: %s ms.", trajanje));
} catch (Exception e){
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("[] Count: "+count);
count++;
}
}
}
The thing is working when running locally on my Windows machine, but when it is running on the remote Jenkins linux machine the refresh doesn't seem to work.
This is the Jenkins console output:
16:18:48 [INFO] Homepage initialized.
16:18:48 ### A2019-10-30T16:18:48.904
16:18:49 [INFO] Spinner (ili uvjet čekanja) je trajao: 117 ms. (waitForNoSpinner-try)
16:18:49 [INFO] Spinner (ili uvjet čekanja) je trajao: 89 ms. (waitForNoSpinner-finally)
16:18:49 [DEBUG] location reloaded, .ng-scope elements: 0
16:18:49 ### B2019-10-30T16:18:49.417
16:18:49 [DEBUG] broj .ng-scope elemenata: 0
16:18:49 [OK] JpoPO() initialized.
The test fails in the following step when trying to find an element that is not there unless the page is refreshed. This is also confirmed by using screenshots that I'm unable to share.
There are multiple ways of refreshing a page with Selenium here, but none of them is working.
It just seems that neither js.executeScript("location.reload()")
nor driver.navigate().refresh()
are working.
I'm using the following Chromedriver options :
if(System.getProperty("os.name").equals("Linux")){
options.addArguments("--headless");
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
options.addArguments("--window-size=1200,800");
WebDriverManager.chromedriver().version("77.0.3865.40").setup();
}
EDIT:
When I tried refreshing using the Robot class, I would get a java.awt.AWTException: headless environment
so I added System.setProperty("java.awt.headless", "false");
to my code.
Now I'm getting a
java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
I know next to nothing about Linux, can that be the reason for this?