I'm trying to use WebDriver to get the session ID from the browser being tested so that I can pass that ID in through the given().session(string) functionality in RESTAssured. The browser will go through a normal login to create the session ID. Any ideas how to capture it?
You can get your session id by casting the driver object to RemoteWebDriver like this
String session = ((RemoteWebDriver) driver).getSessionId().toString();
Maybe you could just read it from cookie. Or I don't understand the problem.
Here is similar problem Get cookies in Webdriver how?
Related
I would like to clarify how can I use Selenoid API to check downloaded files.
According the documentation to get downloaded file from container I need to call
http://selenoid-host.example.com:4444/download/f2bcd32b-d932-4cdc-a639-687ab8e4f840/myfile.txt
where f2bcd32b-d932-4cdc-a639-687ab8e4f840 is a Selenoid Session ID
So, the question is "How can I get this session ID?"
Value, returned by getSessionId from RemoteWebDriver doesn't look like correct one.
UPD. My bad. Session ID, that I can get from RemoteWebDriver instance, is correct.
The problem was I didn't wait enough to get a file exactly downloaded into container. That's why I got 404 error on attempt to get a file via API
Thats depends on your client bindings that you use for tests.
For example in java:
WebDriver driver = new FirefoxDriver();
SessionId session = ((FirefoxDriver)driver).getSessionId();
System.out.println("Session id: " + session.toString());
If you want to get list of active sessions with their IDs outside of tests: use selenoid /status endpoint
Pleas notice, if you use GGR - it modifies session id -
every new browser session in Selenium automatically obtains an ID
called session ID. According to Selenium JSONWire protocol this ID is
always passed to request. GridRouter appends information about
selected Selenium Hub to this session and returns enriched session ID
to user.
https://hackernoon.com/selenium-testing-a-new-hope-7fa87a501ee9
How can I pass a chrome process ID to selenium to force it to start an existing browser? I tried passing arguments to capabilities but they did not help.
Thanks
Any help is appreciated.
capabilities.setCapability( "pid", pid ); //$NON-NLS-1$
ChromeDriver driver = new ChromeDriver(capabilities);
Answering straight, No, you can't pass a Chrome Process ID [PID] to Selenium to force it to start an existing browser.
Similarly, even if you are able to extract the Session ID and other session attributes from the previous Browsing Session still you won't be able to pass those attributes as a HOOK to the WebDriver.
I have a class under which I have different methods one of them is login and other are related to the adding product to cart, checkout and shipping.
When I try to run the methods using TestNG in one go, it execute all the methods in different browsers session and I lost my login session.
I want some solution so that either all the methods execute in same browser or I can use the session of login method in other consecutive methods
Thanks in advance
I have same login for both
I am creating new instance of driver in both methods
Here is my code :
public class purchase {
#Test
public void login(){
System.setProperty("webdriver.chrome.driver", "{path}/chromedriver.exe");
WebDriver fd= new ChromeDriver();
fd.get("{domain}/login/");
/*{login script here}*/
}
#Test
public void purchaseItem(){
System.setProperty("webdriver.chrome.driver", "{path}/chromedriver.exe");
WebDriver fd1= new ChromeDriver();
fd1.get("{domain}/travel");
/*add item to cart*/
}
}
It means there might be 2 issue:
1. You have separate login in both the methods
2. You are creating new instance of driver in both methods.
Please share your code and will help you with resolving it.
You have to share the same driver instance between methods. Otherwise, you will have a new session each time, like you described it.
Here you go.. as per your comment - "I am creating new instance of driver in both methods"..
So, if you want to maintain the session, you have to use a single instance of driver in both methods.
You can achieve it using beforeClass method (Use it for driver initializing and login), and write your logic in different test methods.. So it will make sure complete test cycle for the class is 1 and then your session will not be lost.
Let me know if this helps.
I want to use webdriver in nodejs to control a website which use ajax very heavy, especially it always have http request to server.
When I use driver.executeScript , I found the promise returned almost never resovled.
I checked the website, found that it use a keep-alive http request loop to communicate with server. That means, it will always have at least one live connection to server for 30s, then another connection for another 30s, again and again. which cause document.readyState keep interactive instead of completed, and then driver.executeScript almost be blocked forever.
I had tried driver.manage().timeouts().pageLoadTimeout(PAGE_LOAD_TIMEOUT_MS) but it only throws a exception after timeout.
I also tried to stop the connections by press ESC on the browser window. after that, it seems driver.executeScript can run immediately. But I didn't find any function like window.stop() in WebDriver API.
so is there have a way to resolve this problem? Either should be ok like:
run executeScript right now, regardless of whether page is loaded;
a webdriver api like driver.browser.stop() could stop all live connections in page.
Nodejs code to re-produce this problem:
const WebDriver = require('selenium-webdriver')
const driver = new WebDriver.Builder()
.withCapabilities(
WebDriver.Capabilities.firefox()
.set('webdriver.load.strategy', 'unstable')
).build()
driver.get('https://wx.qq.com/')
driver.executeScript('return "99% will be blocked, 1% luck to return"')
.then(function (ret) {console.log(ret)})
thanks!
UPDATE:
I found maybe set webdriver.load.strategy to unstable in firefox will be help.
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("webdriver.load.strategy", "unstable"); WebDriver
driver = new FirefoxDriver(fp);
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/4993
https://w3c.github.io/webdriver/webdriver-spec.html#page-load-strategies-1
Links from others got same problem:
http://grokbase.com/t/gg/webdriver/1263dbyws6/how-to-click-stop-button-or-equivalent
I think that if you don't use Implicit time the driver won't wait till it's loaded so you'll be able to execute your script.
If this don't work you can try to use your own timer. Check if doc state is ready, if it's not ready after some timeout then stop it manually (by JS) and run your script.
By using Thread.sleep(secs);. For example, we want to load a page in 3 secs means we write thread.sleep(30000);.
I am migrating from RC to webdriver.
In my existing project I use methods from the Selenium Class like
selenium.click()
selenium.type()
etc.
Do I need to change these to the equivalent webdriver commands, or is there a way i can still use these commands?
I use firefox 12, Eclipse IDE
There is the WebDriverBackedSelenium. Essentially this is a bridge between the RC API and WebDriver API. This will do what you are after, there will be some modification to code, but majority will still be the same. It gives you the flexibility of the WebDriver itself, while keeping old code the same.
It is highly recommended to fully convert your solution to use the WebDriver API directly.
The WebDriver API is constantly being updated, worked on and supported.
The RC API and the "RC-WebDriver-Bridge" (WebDriverBackedSelenium) won't be.
Page on WebDriverBackedSelenium exists here:
http://seleniumhq.org/docs/03_webdriver.html#alternative-back-ends-mixing-webdriver-and-rc-technologies
Sample usage to create a new instance of Firefox:
var driver = new FirefoxDriver();
var selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
selenium.stop();
After creating a WebDriverBackedSelenium instance with a given Driver, one does not have to call start() - as the creation of the Driver already started the session. At the end of the test, stop() should be called instead of the Driver's quit() method.
This is more similar to WebDriver's behaviour - as creating a Driver instance starts a session, yet it has to be terminated explicitly with a call to quit().