This page will help you in upgrading your existing Selenium WebDriver tests to use Selenium 4, with the WebDriver W3C Protocol.
TestingBot has full support for this new Selenium version. Below is more information on how to make this transition.
Updating your tests
With the new W3C protocol, some restrictions apply to using capabilities with your test.
Before the W3C protocol (the JSONWP protocol), you could simply add all our custom TestingBot capabilities to your test in the desiredCapabilities.
With the new W3C protocol, only some W3C WebDriver capabilities are allowed inside the capabilities:
browserName
browserVersion
platformName
acceptInsecureCerts
pageLoadStrategy
proxy
timeouts
unhandledPromptBehavior
All other capabilities are treated as Extension capabilities and should be namespaced (include a vendor prefix).
For TestingBot specific capabilities, you'll need to wrap all capabilities inside a tb:options namespace.
Please see the table below on how the capabilities need to be changed:
Capability JSONWP
Capability W3C
browserName
browserName
version
browserVersion
platform
platformName
selenium-version, chromedriverVersion, extra, nameand more
importunittestimportsysfromseleniumimportwebdriverfromselenium.webdriver.common.keysimportKeysfromselenium.webdriver.common.desired_capabilitiesimportDesiredCapabilitiesfromtestingbotclientimportTestingBotClientclassTestTestingBotClient(unittest.TestCase):defsetUp(self):desired_cap={'browserName':'chrome','version':'latest-1','platform':'WIN10','screenrecorder':true,'build':'testbuild','name':'testname',}self.driver=webdriver.Remote(command_executor='http://key:secret@hub.testingbot.com/wd/hub',desired_capabilities=desired_cap)deftest_google_example(self):self.driver.get("http://www.google.com")ifnot"Google"inself.driver.title:raiseException("Unable to load google page!")elem=self.driver.find_element_by_name("q")elem.send_keys("TestingBot")elem.submit()deftearDown(self):self.driver.quit()status=sys.exc_info()==(None,None,None)tb_client=TestingBotClient('key','secret')tb_client.tests.update_test(self.driver.session_id,self._testMethodName,status)if__name__=='__main__':unittest.main()
importunittestimportsysfromseleniumimportwebdriverfromselenium.webdriver.common.keysimportKeysfromselenium.webdriver.common.desired_capabilitiesimportDesiredCapabilitiesfromtestingbotclientimportTestingBotClientclassTestTestingBotClient(unittest.TestCase):defsetUp(self):desired_cap={'browserName':'chrome','browserVersion':'latest-1','platformName':'WIN10','tb:options':{'screenrecorder':true,'build':'testbuild','name':'testname','selenium-version':'3.11.0'}}self.driver=webdriver.Remote(command_executor='http://key:secret@hub.testingbot.com/wd/hub',desired_capabilities=desired_cap)deftest_google_example(self):self.driver.get("http://www.google.com")ifnot"Google"inself.driver.title:raiseException("Unable to load google page!")elem=self.driver.find_element_by_name("q")elem.send_keys("TestingBot")elem.submit()deftearDown(self):self.driver.quit()status=sys.exc_info()==(None,None,None)tb_client=TestingBotClient('key','secret')tb_client.tests.update_test(self.driver.session_id,self._testMethodName,status)if__name__=='__main__':unittest.main()
Print the current page as a PDF, available in Chrome, Edge and Firefox.
You can pass additional options to customize the PDF, such as page size, range, margins, background and shrink to fit.
Selenium 4 now allows you to query the driver for the current timeout values.
Please see the examples below on how to do this, for example with implicit waits, pageload timeouts or script timeout.
Firefox implemented a feature to take screenshots of the entire page, not just the viewport, which is the default setting for other browsers or for Selenium 3.
Before Selenium 4, it was only possible to set preferences (through capabilities) at the start of a test.
Firefox now provides the option to change preferences during a session. You can do this by toggling the context between chrome and content.
Make sure to switch back to the correct context, or some commands might fail.
This feature is not yet supported in the Python bindings.
IHasCommandExecutorhasCommandExecutor=DriverasIHasCommandExecutor;vargetContextCommandInfo=newHttpCommandInfo(HttpCommandInfo.GetCommand,"/session/{sessionId}/moz/context");varsetContextCommandInfo=newHttpCommandInfo(HttpCommandInfo.PostCommand,"/session/{sessionId}/moz/context");hasCommandExecutor.CommandExecutor.TryAddCommand("getContext",getContextCommandInfo);hasCommandExecutor.CommandExecutor.TryAddCommand("setContext",setContextCommandInfo);SessionIdsessionId=((RemoteWebDriver)Driver).SessionId;Driver.Navigate().GoToUrl("https://www.google.com");varelement=Driver.FindElement(By.CssSelector("#gws-output-pages-elements-homepage_additional_languages__als"));Assert.IsTrue(element.Text.Contains("offered in"));try{Driver.ExecuteJavaScript("Services.prefs.setStringPref('intl.accept_languages', 'es-ES')");Assert.Fail("Can not change Service prefs in content context, so previous method should fail");}catch(WebDriverException){// This is expected}vargetContextCommand=newCommand(sessionId,"getContext",null);Responseresponse=hasCommandExecutor.CommandExecutor.Execute(getContextCommand);Assert.AreEqual("content",response.Value);Dictionary<String,Object>payload=newDictionary<string,object>();payload.Add("context","chrome");varsetContextCommand=newCommand(sessionId,"setContext",payload);hasCommandExecutor.CommandExecutor.Execute(setContextCommand);response=hasCommandExecutor.CommandExecutor.Execute(getContextCommand);Assert.AreEqual("chrome",response.Value);Driver.ExecuteJavaScript("Services.prefs.setStringPref('intl.accept_languages', 'es-ES')");try{Driver.Navigate().Refresh();Assert.Fail("Can not navigate in chrome context, so previous method should fail");}catch(WebDriverException){// This is expected}payload.Remove("context");payload.Add("context","content");setContextCommand=newCommand(sessionId,"setContext",payload);hasCommandExecutor.CommandExecutor.Execute(setContextCommand);Driver.Navigate().Refresh();element=Driver.FindElement(By.CssSelector("#gws-output-pages-elements-homepage_additional_languages__als"));Assert.IsTrue(element.Text.Contains("Ofrecido por"));
BiDirectional Functionality
The WebDriver BiDirectional Protocol is a new protocol enhancement that should improve the speed and reliability of Webdriver tests. Its mechanism is similar to CDP (Chrome DevTools Protocol), but is implemented as a vendor agnostic protocol.
At this point, TestingBot does not yet providi BiDi support. We are working on adding this functionality in Q2 of 2023.
Upgrading dependencies
Please see the examples below on how to upgrade to Selenium 4.
Before Selenium 4 you could merge capabilities, mutating the calling object.
Since Selenium 4 this is deprecated, you now need to manually assign the result of the merge operation.