1

I have an automated script where I am trying to go to a page and check that I cannot download a report if I am not signed in. My test steps are:

GIVEN that I am an anonymous user on the WTR
WHEN I navigate to the Reports Centre tool
AND select a 'Download' button on a report
THEN the login or subscribe gate will be displayed
AND if I click the 'Subscribe' button
THEN the Subscribe page will be displayed
AND when I navigate back to the Reports Centre tool
AND select a 'Download' button on a report
THEN the login or subscribe gate will be displayed
AND if I click the 'Login' button
THEN the Login page will be displayed

However when I run my script, I always get the following error: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

Here is my stacktrace:

  (Session info: chrome=81.0.4044.92)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'd038d2fb3cc5', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.76-linuxkit', java.version: '11.0.7'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 81.0.4044.92, chrome: {chromedriverVersion: 81.0.4044.69 (6813546031a4b..., userDataDir: /tmp/.com.google.Chrome.h0SS5v}, goog:chromeOptions: {debuggerAddress: localhost:46475}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true, webdriver.remote.sessionid: 073a630f009a019a022a5d9a2b2...}
Session ID: 073a630f009a019a022a5d9a2b268f81
*** Element info: {Using=xpath, value=//div[@id='search-ui']/div/div[2]/div[3]/div[2]/div/div/div/div[5]/button/span}
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at com.lbr.testcases.gxr.wtr.gating.anon.gating_wtr_reports_centre_anon.reportscentre(gating_wtr_reports_centre_anon.java:33)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464) 

I have a java screenshot utility that executes before ending the test. At first I could see the app had not fully loaded so I added an explicit wait. Then it looked like a cookie banner was blocking the click so I have added a click to the accept button on the banner but I am still seeing this banner in my screenshot. This method has worked when I had similar errors on other scripts.

I have seen: org.openqa.selenium.NoSuchElementException: no such element and I have tried the implicit wait which did not work. I am currently back to using the explicit wait. I have not tried the method ordering as I am not using Intelliji. Are there any other suggestions? I am new to Java ...

Here is my script:

package com.lbr.testcases.gxr.wtr.gating.anon;

import org.junit.Test;
import org.junit.Before;
import org.hamcrest.MatcherAssert;
import org.junit.After;
import static org.hamcrest.CoreMatchers.is;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import java.util.*;
import com.lbr.utils.Screenshot;
import java.net.MalformedURLException;
import java.net.URL;
public class gating_wtr_reports_centre_anon {
  private WebDriver driver;
  JavascriptExecutor js;

  @Before
  public void setUp() throws MalformedURLException {
    driver = new RemoteWebDriver(new URL("localhost"), DesiredCapabilities.chrome());
    js = (JavascriptExecutor) driver;
    new HashMap<String, Object>();
  }
  @After
  public void tearDown() {
    Screenshot.TakeScreenshot(driver);
    driver.quit();
  }
  @Test
  public void gatingwtrreportscentreanon() {
    // GIVEN that I am an anonymous user on the WTR
    // WHEN I navigate to the Reports Centre tool
    driver.get("https://www.worldtrademarkreview.com/tools/reports-centre/?page=1&ps=20");
    new WebDriverWait(driver, (30)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']"))); 
    driver.findElement(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']")).click();
    // AND select a 'Download' button on a report
    driver.findElement(By.cssSelector(".report-search-results .lexcomponent-card:nth-child(1) .download-text")).click();
    // THEN the login or subscribe gate will be displayed
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h1[contains(.,\'ACCOUNT LOGIN\')]")).getText(), is("ACCOUNT LOGIN"));
    // AND if I click the 'Subscribe' button
    driver.findElement(By.linkText("Subscribe")).click();
    // THEN the Subscribe page will be displayed
    MatcherAssert.assertThat(driver.findElement(By.cssSelector(".text--upper")).getText(), is("WHY SUBSCRIBE?"));
    // AND when I navigate back to the Reports Centre tool 
    driver.get("https://www.worldtrademarkreview.com/tools/reports-centre/?page=1&ps=20");
    new WebDriverWait(driver, (30)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']"))); 
    driver.findElement(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']")).click();
    // AND select a 'Download' button on a report
    driver.findElement(By.cssSelector(".report-search-results .lexcomponent-card:nth-child(1) .download-text")).click();
    // THEN the login or subscribe gate will be displayed
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h1[contains(.,\'ACCOUNT LOGIN\')]")).getText(), is("ACCOUNT LOGIN"));
    // AND if I click the 'Login' button
    driver.findElement(By.linkText("Login")).click();
    // THEN the Login page will be displayed
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h1[contains(.,\'ACCOUNT LOGIN\')]")).getText(), is("ACCOUNT LOGIN"));
  }
}
Dilip Meghwal
  • 632
  • 6
  • 15

0 Answers0