List of usage examples for org.openqa.selenium WebDriver manage
Options manage();
From source file:com.mycompany.newseleniumtest.coba.java
public void latihan(String browser) throws InterruptedException { //Instantiate the webdriver object: WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor) driver; //Open the web driver.get("http://dev.uangteman.com/admin/"); driver.manage().window().maximize(); Thread.sleep(1500);/*from w ww .jav a 2 s .co m*/ driver.findElement(By.name("bu_name")).sendKeys("rahmat_cs"); driver.findElement(By.name("bu_passwd")).sendKeys("testing"); driver.findElement(By.cssSelector("button[class='btn btn-orange btn-squared pull-right ladda-button']")) .click(); Thread.sleep(2500); jse.executeScript("window.scrollBy(0,1500)", ""); jse.executeScript("window.scroll(0,1500)", ""); WebElement scroll = driver.findElement(By.xpath( "/html/body/div[2]/div[2]/div/div[2]/div/div/div[3]/div/div[2]/div/form/table/tbody/tr[1]/td[12]/span")); JavascriptExecutor js = (JavascriptExecutor) driver; jse.executeScript( "document.getElementByxpath('/html/body/div[2]/div[2]/div/div[2]/div/div/div[3]/div/div[2]/div/form/table/tbody/tr[1]/td[12]/span').scrollLeft += 250", ""); //Get page title in selenium webdriver String actual = driver.getTitle(); }
From source file:com.mycompany.seleniumtest.Selenium2Example.java
public static void main(String[] args) { // Create a new instance of the Firefox driver // Notice that the remainder of the code relies on the interface, // not the implementation. System.setProperty("webdriver.chrome.driver", "/Users/CosticaTeodor/Downloads/drivers/chromedriver"); System.setProperty("webdriver.safari.driver", "/usr/bin/safaridriver"); //WebDriver driver = new ChromeDriver(); WebDriver driver = new ChromeDriver(); // And now use this to visit the cars program driver.get("http://localhost:3000/"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.google.com"); //verify the page is loaded, wait 10 seconds if it doesn't try {/*from w ww . j a va 2 s .c o m*/ driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); System.out.println("Page loaded!"); } catch (Exception e) { System.err.println(e.getMessage()); System.err.println(e.getStackTrace()); } int rowCount = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); if (rowCount == 5) { System.out.println(rowCount + " is the row count!"); } else { System.out.println("rowcount should be different!"); } // Find the filter input element by its name WebElement searchFilter = driver.findElement(By.id("filter")); // Enter 2002 to search for searchFilter.sendKeys("2002"); //check if there are two rows left rowCount = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); System.out.println("Row count after filter is: " + rowCount); searchFilter.clear(); searchFilter.sendKeys(""); rowCount = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); System.out.println("Row count after filter is: " + rowCount); //Close the browser driver.quit(); }
From source file:com.mycompany.seleniumwebdriverutilities2.BrowserResizerUtility.java
/** * Resizes the browser controlled by the WebDriver driver * sourced from http://stackoverflow.com/questions/16664433/how-to-resize-current-browser-window-in-selenium-webdriver-with-java * @param browser/* w w w . ja v a 2 s . c o m*/ * @param xDim * @param yDim */ public static void resizeBrowser(WebDriver browser, int xDim, int yDim) { Dimension dimension = new Dimension(xDim, yDim); browser.manage().window().setSize(dimension); }
From source file:com.pages.CompanyLoginpage.java
public static void CompanyLogin(WebDriver driver, String Companynumber) throws InterruptedException { String url = driver.getCurrentUrl(); if (URL.equalsIgnoreCase(url)) { driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); common.Wait_Until_ElementVisible(driver, LicenseAgreement_popup); driver.findElement(By.xpath("//button[@id='btnEULAAgree']")).click(); //Thread.sleep(1000); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); } else {//from w w w .j a va 2 s . c o m driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(String.format("window.localStorage.clear();")); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().deleteAllCookies(); Actions act = new Actions(driver); act.sendKeys(Keys.CONTROL.F5).perform(); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.findElement(By.xpath("//button[@id='btnEULAAgree']")).click(); //Thread.sleep(1000); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS); } JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("return document.readyState").equals("complete"); //Thread.sleep(1000); WebDriverWait ww = new WebDriverWait(driver, 30); ww.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(AccessKey_fld_xpath))); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.findElement(By.xpath(AccessKey_fld_xpath)).sendKeys(Companynumber); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.findElement(By.xpath(Download_btn_xpath)).click(); driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS); // Thread.sleep(500); ww.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(HomePage_NavBar))); }
From source file:com.pages.CompanyLoginpage.java
public static void VerifyAccessKeyfld(WebDriver driver, String Companynumber) throws InterruptedException { driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().deleteAllCookies();//from w w w .jav a 2s . com Thread.sleep(1000); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("return document.readyState").equals("complete"); WebElement AccessKeyfld = driver.findElement(By.xpath(AccessKey_fld_xpath)); if (AccessKeyfld.isDisplayed() || AccessKeyfld.isEnabled()) { driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.findElement(By.xpath(AccessKey_fld_xpath)).sendKeys(Companynumber); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.findElement(By.xpath(AccessKey_fld_xpath)).clear(); System.out.println("AccessKey field is displayed and enabled"); } else { AssertJUnit.fail("AccessKeyfld is not Displayed"); } }
From source file:com.pages.CompanyLoginpage.java
public static void VerifyDownLoadBtn(WebDriver driver) { driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement DownLoadBtn = driver.findElement(By.xpath(Download_btn_xpath)); if (DownLoadBtn.isDisplayed() || DownLoadBtn.isEnabled()) { driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.findElement(By.xpath(Download_btn_xpath)).click(); System.out.println(//from w w w . j a v a2s.c om "DownLoadBtn is Displayed and Enabled,User is able to Login by clicking the Download button."); } else { AssertJUnit.fail("DownLoadBtn is not Displayed"); } }
From source file:com.pages.CompanyLoginpage.java
public static void Validate_CompanyLogin(WebDriver driver, String InValidCompanyNum) throws IOException { driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.manage().deleteAllCookies();/*w w w. j a va2 s . co m*/ driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("return document.readyState").equals("complete"); //Thread.sleep(1000); WebDriverWait ww = new WebDriverWait(driver, 30); ww.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(AccessKey_fld_xpath))); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); String CompanyNum = getValue("InValidCompanyNum"); if (CompanyNum.equals(InValidCompanyNum)) { driver.findElement(By.xpath(AccessKey_fld_xpath)).sendKeys(CompanyNum); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.findElement(By.xpath(Download_btn_xpath)).click(); String Error1 = driver.findElement(By.xpath(Error_Messagepart1_xpath)).getText(); String Error2 = driver.findElement(By.xpath(Error_Messagepart2_xpath)).getText(); String Error = Error1 + " " + Error2; if (Error.equalsIgnoreCase("ERROR! Invalid company.")) { System.out.println("Error message" + " " + Error + " " + "displayed, result as expected"); } else { AssertJUnit.fail("Error message not displayed"); } } }
From source file:com.partnet.automation.selenium.AbstractConfigurableDriverProvider.java
License:Apache License
/** * This is the default implementation of getting a web driver. * /*from ww w . ja v a2 s .c om*/ * Use this as a quick start guide for * {@link AbstractConfigurableDriverProvider#getWebDriver(Browser)} * * @param browser browser to obtain * @return instance of the specific WebDriver */ protected WebDriver getDefaultWebDriver(Browser browser) { Objects.requireNonNull(browser, "browser cannot be null"); WebDriver driver; boolean useRemoteBrowser = (getRemoteSeleniumUrlString() != null); LOG.debug("Initialize: '{}' using remote browser: '{}'", browser, useRemoteBrowser); switch (browser) { case IE: driver = useRemoteBrowser ? getRemoteInternetExplorerWebDriver() : getInternetExplorerWebDriver(); break; case CHROME: driver = useRemoteBrowser ? getRemoteChromeWebDriver() : getChromeWebDriver(); break; case FIREFOX: driver = useRemoteBrowser ? getRemoteFirefoxWebDriver() : getFirefoxWebDriver(); break; case PHANTOMJS: driver = useRemoteBrowser ? getRemotePhantomJsWebDriver() : getPhantomJsWebDriver(); break; case HTMLUNIT: if (useRemoteBrowser) { // Don't throw here, in case running with a Grid. LOG.info("Running HTMUNIT on the grid is NOT supported! If you want to take the time to create " + "a HTTP connection to the grid, you might as well just use PhantomJs. "); } driver = getHtmlUnitWebDriver(); break; case ANDROID: driver = getRemoteAndroidWebDriver(); break; default: throw new IllegalArgumentException(String.format("Unsupported browser: '%s'", browser.name())); } LOG.debug("Using WebDriver implementation of type : {}", driver.getClass()); if (!browser.isAndroid()) { driver.manage().timeouts().pageLoadTimeout(Integer.getInteger(PAGE_LOAD_TIMEOUT_SECONDS, 120), TimeUnit.SECONDS); } return driver; }
From source file:com.partnet.automation.selenium.AbstractConfigurableDriverProvider.java
License:Apache License
/** * Screenshooter for HTMLUnit. It saves the html source to disk following the * same pattern as the screenshot path. The HTMLUnit session is transfered to * PhantomJs, which takes the screenshot, and is destroyed. The original * driver is not destroyed/*from w ww. ja v a2s.c om*/ * * Note: Javascript events, current page changes, etc.. are not saved and are * not captured in the screenshots taken. * * @param path * - where to save the file. This assumes a png file will be * generated * @param baseUrl * - used to transfer the cookies to the phantomjs driver properly. * * @see #getPhantomJsWebDriver() */ public void saveScreenshotForHtmlUnit(String path, String baseUrl) { final WebDriver driver = this.get(); if (!(driver instanceof HtmlUnitDriver)) { LOG.warn("Wrong driver called screenshooter for HTMLUnit driver, default to regular screenshooter"); this.saveScreenshotAs(path); return; } PhantomJSDriver phantomJs = (PhantomJSDriver) getPhantomJsWebDriver(); try { phantomJs.get(baseUrl); String url = driver.getCurrentUrl(); LOG.debug("Url: {}", url); for (Cookie cookie : driver.manage().getCookies()) { LOG.debug("Cookie: {}", cookie.toString()); phantomJs.manage().addCookie(cookie); } phantomJs.get(url); // set current thread to phantomjs, and take screenshot in the default way this.set(phantomJs); LOG.debug("HTML Screenshot taken: {}", this.saveScreenshotAs(path)); } finally { // set back original driver for this thread this.set(driver); phantomJs.close(); phantomJs.quit(); } }
From source file:com.pentaho.ctools.utils.ElementHelper.java
License:Apache License
/** * This method shall wait for the title and return it. The developer can * specify the timeout and pollingTime./*from w w w . j a v a2 s. c o m*/ * * * @param driver * @param title * @param timeout * @param pollingTime * @return */ public String WaitForTitle(final WebDriver driver, final String title, final Integer timeout, final Integer pollingTime) { this.log.debug("WaitForTitle::Enter"); String returnTitle = ""; driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); ExecutorService executor = null; try { class RunnableObject implements Runnable { private Boolean textIsEquals; public Boolean isTextEquals() { return this.textIsEquals; } @Override public void run() { Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(pollingTime, TimeUnit.MILLISECONDS); // Wait for element visible this.textIsEquals = wait.until(new Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver d) { String currentTitle = driver.getTitle(); return currentTitle != null && currentTitle.contains(title); } }); } } RunnableObject r = new RunnableObject(); executor = Executors.newSingleThreadExecutor(); executor.submit(r).get(timeout + 2, TimeUnit.SECONDS); if (r.isTextEquals()) { // If the text is equals then send the text that we wait for. returnTitle = title; this.log.debug("Wait for text successful!"); } } catch (InterruptedException ie) { this.log.warn("Interrupted Exception"); this.log.warn(ie.toString()); } catch (ExecutionException ee) { if (ee.getCause().getClass().getCanonicalName() .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) { this.log.warn("WebDriver timeout exceeded!"); } else { this.log.warn("Execution Exception"); this.log.warn(ee.toString()); } } catch (java.util.concurrent.TimeoutException cte) { this.log.warn("Thread timeout exceeded!"); this.log.warn(cte.toString()); } catch (Exception e) { this.log.error("Exception"); this.log.catching(e); } if (executor != null) { executor.shutdown(); } driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); this.log.debug("WaitForTitle::Exit"); return returnTitle; }