org.openqa.selenium.remote.SessionNotFoundException: session null does not exist - selenium

I'm trying integrate selenium with spring. The code below works perfect if no spring dependencies added in pom.xml. but if I add spring boot dependency like below(I didn't add cucumber spring here and I removed all springcontext xml, cusumber xml from eclipse workspace) and run same test without modifying anything, It opens a IE test window http://localhost:36359/ then error out in console like below "org.openqa.selenium.remote.SessionNotFoundException: session null does not exist (WARNING: The server did not provide any stacktrace information)".
I even tried adding all spring annotation replacing #Before method by initizing webdriver in springcontext; but still the same behavior. can someone help me to fix this issue. I'm using selenium 3.4.0 version
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.4.3.RELEASE</version>
<relativePath />
</parent>
public class ScenarioOutlineStepDef {
WebDriver driver;
#Before()
public void setUp() {
System.setProperty("webdriver.ie.driver", "C:/IEDriverServer-64.exe");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
dc.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
dc.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
dc.setJavascriptEnabled(true);
dc.setBrowserName("internet explorer");
driver = new InternetExplorerDriver(dc);
}
#Given("^user navigates to Pricing Portal$")
public void goToPricingPortal() {
driver.navigate().to(
"xyz.com;
}
#When("^I enter Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$")
public void I_enter_Username_as_and_Password_as(String arg1, String arg2) {
driver.findElement(By.id("txtUserDefault")).sendKeys(arg1);
driver.findElement(By.id("txtPassDefault")).sendKeys(arg2);
driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
}
#Then("^login should be unsuccessful$")
public void validateRelogin() {
if (driver.getCurrentUrl().equalsIgnoreCase(
"xyz.com")) {
System.out.println("Test Pass");
} else {
System.out.println("Test Failed");
}
// driver.close();
}

Do it manually
Set same Security level for all zones. Try this steps
Open Internet Explorer Browser
Go to menu and open Tools -> Internet Options -> Security
Set all values of zones (Internet, Local intranet, Trusted sites, Restricted sites) to the same protected mode, enabled or disabled should not matter
click on OK.
OR use DesiredCapabilities like this
DesiredCapabilities IEcaps = DesiredCapabilities.internetExplorer();
IEcaps .setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
File SrcFile= new File("E:\\Sankalp\\IE\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", SrcFile.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver(IEcaps );
driver.get("Your URL");
driver.quit();

Related

Selenium JUnit tests not running org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?

I have created a java/selenium/cucumber automation framework with 4 feature files each testing different functional area's.
I can run each of these within Eclipse as feature files and they all run fine.
However I am trying to figure out how I can run them all as a JUnit test so that I can generate the extent reports.
I run the mainRunner class as a Junit test like ..
The first feature runs fine but then the next three do not. A chrome browser is opened for each of these but
the site is not navigated to. The error
'org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?'
is shown in the console logs.
So is this issue to do with how I have set my feature files up to use the startBrowser, initBrowser and closeBrowser methods. Which are
defined in a seperate class 'BrowserInitTearDownStepDefinitions'?
All my feature files look like ..
Feature: Update a Computer in Database
-- left out details
#StartBrowser
Scenario: Start browser session
When I initialize driver
Then open browser
#MyTest4
Scenario Outline: Update a computer
-- left our details
Examples:
-- left out details
#CloseBrowser
Scenario: Close the browser
Then close browser
I have had a look at the other posts related to this message that reference the same error, but can't see a clear cut solution.
As the error would indicate I seem to be quitting the driver but then not reinitialising it for the next test. Although Im not sure why this
would be the case given that each feature file has the #StartBrowser and #CloseBrowser tags which should execute these methods. Below is my BrowserInitTearDownStepDefinitions class.
public class BrowserInitTearDownStepDefinitions {
//Initialises Chrome browser
#When("^I initialize driver$")
public void initializeDriver() {
System.out.println("initializeDriver runs");
System.setProperty("webdriver.chrome.driver","C:\\xxxx\\CucumberAutomationFramework\\src\\test\\java\\cucumberAutomationFramework\\resources\\chromedriver.exe");
WebDriverUtils.setDriver(new ChromeDriver());
}
//Opens Chrome browser and clicks in search input box
#Then("^open browser$")
public void openBrowser() {
System.out.println("Open Browser runs");
WebDriver driver = WebDriverUtils.getDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
driver.get("https://computer-database.gatling.io/computers");
}
//Closes browser after all scenarios run and deletes cookies
#Then("^close browser$")
public void closeBrowser() {
System.out.println("Close Browser runs");
WebDriver driver = WebDriverUtils.getDriver();
driver.manage().deleteAllCookies();
driver.quit();
driver =null;
}
}
I create a new instance of the webdriver within my stepDefs file with ..
WebDriver driver = WebDriverUtils.getDriver()
The webdriver utils class code is ..
package cucumberAutomationFramework.utilityClasses;
import org.openqa.selenium.WebDriver;
public class WebDriverUtils {
private static WebDriver driver;
public static void setDriver(WebDriver webDdriver) {
if (driver == null) {
driver = webDdriver;
}
}
public static WebDriver getDriver() {
if (driver == null) {
throw new AssertionError("Driver is null. Initialize driver before calling this method.");
}
return driver;
}
}
Any advice would be much appreciated. Thanks.

Selenium Parallel execution

Hey guys i am running parallel operations in headless mode or without headless mode i get the same error, which are the following :
INFO: Using parallel execution mode 'CONCURRENT' set via the 'junit.jupiter.execution.parallel.mode.default' configuration parameter.
stale element reference: element is not attached to the page document
stale element reference error is a WebDriver error that occurs because the referenced web element is no longer attached to the DOM. ... When an element is no longer attached to the DOM, i.e. it has been removed from the document or the document has changed, it is said to be stale
How can i fix this problem?
My test configs
test {
systemProperty("junit.jupiter.execution.parallel.enabled", true)
systemProperty("junit.jupiter.execution.parallel.mode.default", "CONCURRENT")
useJUnitPlatform()
}
and my webdriver initialisation
protected WebDriver driver;
protected String TARGET_URL;
#BeforeEach
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(new ChromeOptions());
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().window().maximize();
loginToEnvironment();
}
#AfterEach
public void tearDown() {
driver.manage().deleteAllCookies();
driver.close();
}

The url is not getting loaded completely, getting an error as "[SEVERE]: Timed out receiving message from renderer

The url is not getting loaded completely, getting an error as "[SEVERE]: Timed out receiving message from renderer: -0.010" with below configurations:
Please pardon me since new to this.
Browser: Chrome 64
Selenium: 3.10.0
Build tool: maven (pom.xml)
Testng: 6.8
Scenario:
Use OWASP ZAP APIs with selenium functional test cases. below is the script:
public void login() {
// driver.get(BASE_URL);
driver.navigate().to(BASE_URL);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[#class='btn btn-link']")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#BeforeTest
public void setup() {
zapScanner = new ZAProxyScanner(ZAP_PROXYHOST,ZAP_PROXYPORT,ZAP_APIKEY);
zapScanner.clear(); //Start a new session
zapSpider = (Spider)zapScanner;
log.info("Created client to ZAP API");
driver = DriverFactory.createProxyDriver("chrome",createZapProxyConfigurationForWebDriver(), CHROME_DRIVER_PATH);
// driver = DriverFactory.createProxyDriver("firefox",createZapProxyConfigurationForWebDriver(), FIREFOX_DRIVER_PATH);
myApp = new MyAppNavigation(driver);
// myApp.registerUser(); //Doesn't matter if user already exists, bodgeit just throws an error
System.out.println("before test is done");
}
#Test
public void testSecurityVulnerabilitiesAfterLogin() {
System.out.println("started test");
myApp.login();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// myApp.navigateAfterLogin();
log.info("Spidering...");
spiderWithZap();
log.info("Spider done.");
}
Chrome browser is launched with the given base_url however it the url doesn't load completely and gives an error as shown in the attached screen shot.

How to implement headless browser for secure browser (HTTPS) or validate certificate by utilizing PhantomJs in Selenium?

I need to implement Headless Browser for HTTPS (validate certificate). For this I need to write extra line of code.
I have written for browser HTTP and it is working fine.
public class Headless {
public static void main(String[] args)
{
File src=new File("C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path",src.getAbsolutePath());
WebDriver driver=new PhantomJSDriver();
driver.get("https://www.google.co.in/");
System.out.println(driver.getTitle());
}
}
I google it and get some info:-
phantomjs --ignore-ssl-errors=yes;
DesiredCapabilities dcap = new DesiredCapabilities();
String[] phantomArgs = new String[] {
"--ssl-protocol=any",
"--ignore-ssl-errors=true"
};
dcap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcap);
You can read more about the command-line options on this link: Command Line Interface | PhantomJS

WebDriver not opening URL

I'm very new to selenium so I'm having trouble spotting the problem with my code. I'm using a webDriver backed selenium object, it starts the driver but never opens the URL and the driver just closes after a few moments. The last time this happened to me it was just because I had left "http" out of the URL. So what's causing it this time?
public void testImages() throws Exception {
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.testsite.com/login");
System.out.println(selenium.getXpathCount("//img"));
}
The setup looks like:
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(2000);
}
The teardown method just consists of driver.close().
I'm using selenium 2.14 and the testNG Eclipse plug-in.
You might need to do the following
selenium.open("www.testsite.com/login");
Check out this example from the selenium site:
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();
link to selenium
You would need to add driver.get(url) like below.
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.testsite.com/login");
}