JavascriptExecutor is real useful Interface in webdriver software testing tool. Interface JavascriptExecutor helps you lot to execute javascript inwards your software essay out instance whenever required. If you lot knows/remember, We had seen Different examples of executing javascript inwards selenium IDE to perform unlike actions. Let me laissez passer on you lot 1 instance of executing javascript inwards selenium WebDriver software testing tool.
Sometimes inwards your essay out case, you lot needs to shop your software spider web application page title to compare it amongst expected page title. In selenium IDE, nosotros tin endure "storeTitle" ascendance to shop it inwards variable. In webdriver ,we tin create it straight using driver.getTitle(); every bit shown inwards this example. But if you lot wants to create it using javascript in addition to hence how volition you lot create it?
Example : Get page championship using javascript inwards selenium webdriver
@Test populace void essay out () { JavascriptExecutor javascript = (JavascriptExecutor) driver; //Get electrical current page title String pagetitle=(String)javascript.executeScript("return document.title"); System.out.println("My Page Title Is : "+pagetitle); //Get electrical current page URL String CurrentURL = driver.getCurrentUrl(); System.out.println("My Current URL Is : "+CurrentURL); }
(View more JavascriptExecutor examples inwards webdriver)
In higher upwardly example, I accept used JavascriptExecutor to execute coffee script inwards selenium webdriver software testing tool. Inner javascript volition supply electrical current page championship in addition to shop it inwards variable = pagetitle. Then Next controversy volition impress it inwards the console. You tin endure that variable value to compare amongst your expected page championship if required.
Last ii syntax volition conk electrical current page URLs in addition to Print it inwards console.