List of usage examples for org.openqa.selenium Cookie getName
public String getName()
From source file:com.google.appengine.tck.login.UserLogin.java
License:Open Source License
public void login(@Observes EventContext<Before> event) throws Exception { Before before = event.getEvent();//from w ww .j a v a 2 s. co m UserIsLoggedIn userIsLoggedIn = null; if (before.getTestMethod().isAnnotationPresent(UserIsLoggedIn.class)) { userIsLoggedIn = before.getTestMethod().getAnnotation(UserIsLoggedIn.class); } else if (before.getTestClass().isAnnotationPresent(UserIsLoggedIn.class)) { userIsLoggedIn = before.getTestClass().getAnnotation(UserIsLoggedIn.class); } if (userIsLoggedIn != null) { final URI baseUri = getBaseURI(before.getTestMethod()); final WebDriver driver = createWebDriver(); try { driver.manage().deleteAllCookies(); driver.navigate().to(baseUri + USER_LOGIN_SERVLET_PATH + "?location=" + URLEncoder.encode(userIsLoggedIn.location(), "UTF-8")); // did we navigate to this requested page, or did we get redirected/forwarded to login already List<WebElement> loginUrlElts = driver.findElements(By.id("login-url")); if (loginUrlElts.size() > 0) { String loginURL = loginUrlElts.get(0).getText(); // check if (isInternalLink(loginURL)) { loginURL = baseUri + loginURL; } // go-to login page driver.navigate().to(loginURL); } // find custom login handler, if exists LoginHandler loginHandler = TestBase.instance(getClass(), LoginHandler.class); if (loginHandler == null) { loginHandler = new DefaultLoginHandler(); } loginHandler.login(driver, new UserLoginContext(userIsLoggedIn)); // copy cookies Set<Cookie> cookies = driver.manage().getCookies(); for (Cookie cookie : cookies) { ModulesApi.addCookie(cookie.getName(), cookie.getValue()); } } finally { driver.close(); } } event.proceed(); }
From source file:com.jaeksoft.searchlib.crawler.web.browser.BrowserDriver.java
License:Open Source License
public List<CookieItem> getCookies() { Set<Cookie> cookies = driver.manage().getCookies(); if (CollectionUtils.isEmpty(cookies)) return null; List<CookieItem> cookieList = new ArrayList<CookieItem>(cookies.size()); for (Cookie cookie : cookies) { BasicClientCookie basicCookie = new BasicClientCookie(cookie.getName(), cookie.getValue()); basicCookie.setDomain(cookie.getDomain()); basicCookie.setExpiryDate(cookie.getExpiry()); basicCookie.setPath(cookie.getPath()); basicCookie.setSecure(cookie.isSecure()); cookieList.add(new CookieItem(basicCookie)); }/*from www . jav a2 s . c o m*/ return cookieList; }
From source file:com.lazerycode.ebselen.customhandlers.FileDownloader.java
License:Apache License
/** * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie state * * @param seleniumCookieSet/*from w w w . j a va2s. c o m*/ * @return */ private HttpState mimicCookieState(Set<org.openqa.selenium.Cookie> seleniumCookieSet) { HttpState mimicWebDriverCookieState = new HttpState(); for (org.openqa.selenium.Cookie seleniumCookie : seleniumCookieSet) { Cookie httpClientCookie = new Cookie(seleniumCookie.getDomain(), seleniumCookie.getName(), seleniumCookie.getValue(), seleniumCookie.getPath(), seleniumCookie.getExpiry(), seleniumCookie.isSecure()); mimicWebDriverCookieState.addCookie(httpClientCookie); } return mimicWebDriverCookieState; }
From source file:com.liferay.faces.bridge.test.integration.demo.JSFExportPDFPortletTester.java
License:Open Source License
@Test public void runJSFExportPDFPortletTest() throws IOException { // Test that the view contains links to all three pdfs. BrowserDriver browserDriver = getBrowserDriver(); browserDriver.navigateWindowTo(BridgeTestUtil.getDemoPageURL("jsf-pdf")); WaitingAsserter waitingAsserter = getWaitingAsserter(); waitingAsserter.assertElementDisplayed( "//td[contains(text(),'Green')]/preceding-sibling::td/a[contains(text(),'Export')]"); waitingAsserter.assertElementDisplayed( "//td[contains(text(),'Kessler')]/preceding-sibling::td/a[contains(text(),'Export')]"); String shearerPDFLinkXpath = "//td[contains(text(),'Shearer')]/preceding-sibling::td/a[contains(text(),'Export')]"; waitingAsserter.assertElementDisplayed(shearerPDFLinkXpath); // Test that the "Rich Shearer" link generates a PDF with the correct test. Note: since different browsers // and WebDriver implementations handle downloading files differently, download the file using a Java URL // connection. WebElement shearerPDFLinkElement = browserDriver.findElementByXpath(shearerPDFLinkXpath); String shearerPDFLink = shearerPDFLinkElement.getAttribute("href"); URL shearerPDFURL = new URL(shearerPDFLink); HttpURLConnection httpURLConnection = (HttpURLConnection) shearerPDFURL.openConnection(); httpURLConnection.setRequestMethod("GET"); Set<Cookie> cookies = browserDriver.getBrowserCookies(); String cookieString = ""; for (Cookie cookie : cookies) { cookieString += cookie.getName() + "=" + cookie.getValue() + ";"; }/*from w ww .j a v a2 s .c o m*/ httpURLConnection.addRequestProperty("Cookie", cookieString); InputStream inputStream = httpURLConnection.getInputStream(); // Compare the text of the PDFs rather than the files (via a hash such as md5) becuase the portlet generates // slightly different PDFs each time the link is clicked (CreationDate, ModDate, and Info 7 0 R/ID are // different each time). String shearerRichPDFText = getPDFText(inputStream); inputStream = JSFExportPDFPortletTester.class.getResourceAsStream("/Shearer-Rich.pdf"); String expectedShearerRichPDFText = getPDFText(inputStream); logger.info("Expected Shearer-Rich.pdf text:\n\n{}\nDownloaded Shearer-Rich.pdf text:\n\n{}", expectedShearerRichPDFText, shearerRichPDFText); Assert.assertEquals( "The downloaded Shearer-Rich.pdf file's text does not match the expected Shearer-Rich.pdf file's text.", expectedShearerRichPDFText, shearerRichPDFText); }
From source file:com.liferay.faces.bridge.test.integration.issue.primefaces.ExporterComponentsTestUtil.java
License:Open Source License
static void runDownloadUsersCSVFileTest(BrowserDriver browserDriver, String buttonId) throws IOException { // TECHNICAL NOTE: Since browsers handle file downloads differently, download files with a plain // HttpURLConnection in order to avoid handling each browser case. String buttonXpath = "//form/button[contains(@id,':" + buttonId + "')]"; browserDriver.waitForElementEnabled(buttonXpath); String formXpath = buttonXpath + "/.."; WebElement formElement = browserDriver.findElementByXpath(formXpath); String formActionURLString = formElement.getAttribute("action"); URL formActionURL = new URL(formActionURLString); HttpURLConnection httpURLConnection = (HttpURLConnection) formActionURL.openConnection(); InputStream inputStream = null; try {/*from w ww. ja va 2 s. co m*/ httpURLConnection.setRequestMethod("POST"); httpURLConnection.setUseCaches(false); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.addRequestProperty("Accept", "text/csv"); // TECHNICAL NOTE: Add all cookies from the browser to the HttpURLConnection so that it can imitate the // session of the browser and access the test portlet and files. Set<Cookie> cookies = browserDriver.getBrowserCookies(); String cookieString = ""; for (Cookie cookie : cookies) { cookieString += cookie.getName() + "=" + cookie.getValue() + ";"; } httpURLConnection.addRequestProperty("Cookie", cookieString); // TECHNICAL NOTE: Add all input (and button) name, value pairs to the POST parameters of the // HttpURLConnection so that it can imitate the session of the browser and download the files. List<WebElement> namedElements = new ArrayList<WebElement>( browserDriver.findElementsByXpath(formXpath + "/input")); WebElement button = browserDriver.findElementByXpath(buttonXpath); namedElements.add(button); StringBuilder requestBuilder = new StringBuilder(); boolean first = true; for (WebElement namedElement : namedElements) { if (!first) { requestBuilder.append("&"); } String name = namedElement.getAttribute("name"); requestBuilder.append(URLEncoder.encode(name, "UTF-8")).append("="); String value = namedElement.getAttribute("value"); if (value != null) { requestBuilder.append(URLEncoder.encode(value, "UTF-8")); } first = false; } String requestString = requestBuilder.toString(); httpURLConnection.addRequestProperty("Content-Length", String.valueOf(requestString.length())); String enctype = formElement.getAttribute("enctype"); httpURLConnection.addRequestProperty("Content-Type", enctype); Writer writer = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"); writer.write(requestString); writer.flush(); Assert.assertEquals(HttpURLConnection.HTTP_OK, httpURLConnection.getResponseCode()); String contentDisposition = httpURLConnection.getHeaderField("Content-Disposition"); if (contentDisposition == null) { contentDisposition = httpURLConnection.getHeaderField("Content-disposition"); } if (contentDisposition == null) { contentDisposition = httpURLConnection.getHeaderField("content-disposition"); } String usersCSVFileName; if ((contentDisposition == null) && TestUtil.getContainer().startsWith("pluto")) { usersCSVFileName = buttonId + "Users.csv"; } else { usersCSVFileName = contentDisposition.replace("attachment;filename=", ""); } inputStream = httpURLConnection.getInputStream(); String downloadedUsersCSV = getContents(inputStream); httpURLConnection.disconnect(); logger.info("Expected " + EXPECTED_USERS_CSV_FILE_NAME + " text:\n\n{}\nDownloaded " + usersCSVFileName + " text:\n\n{}", EXPECTED_USERS_CSV, downloadedUsersCSV); Assert.assertEquals( "The downloaded " + usersCSVFileName + " file's text does not match the expected " + EXPECTED_USERS_CSV_FILE_NAME + " file's text.", EXPECTED_USERS_CSV, downloadedUsersCSV); } finally { ClosableUtil.close(inputStream); if (httpURLConnection != null) { httpURLConnection.disconnect(); } } }
From source file:com.machinepublishers.jbrowserdriver.OptionsLocal.java
License:Apache License
/** * {@inheritDoc}/* w w w . j a va2s . co m*/ */ @Override public void deleteCookieNamed(String name) { Cookie toRemove = null; for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) { toRemove = cookie; break; } } if (toRemove != null) { cookies.remove(toRemove); } }
From source file:com.machinepublishers.jbrowserdriver.OptionsLocal.java
License:Apache License
/** * {@inheritDoc}/* ww w . j a va 2s . c om*/ */ @Override public Cookie getCookieNamed(String name) { for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) { return cookie; } } return null; }
From source file:com.machinepublishers.jbrowserdriver.OptionsServer.java
License:Apache License
private org.apache.http.cookie.Cookie convert(Cookie in) { BasicClientCookie out = new BasicClientCookie(in.getName(), in.getValue()); String domainStr = null;//from ww w.j a va2 s .co m if (StringUtils.isEmpty(in.getDomain())) { String urlStr = context.item().engine.get().getLocation(); try { URL url = new URL(urlStr); domainStr = url.getHost(); } catch (MalformedURLException e) { Matcher matcher = domain.matcher(urlStr); if (matcher.matches()) { domainStr = matcher.group(1); } } } out.setDomain(domainStr == null ? in.getDomain() : domainStr); if (in.getExpiry() != null) { out.setExpiryDate(in.getExpiry()); } out.setPath(in.getPath()); out.setSecure(in.isSecure()); out.setValue(in.getValue()); out.setVersion(1); return out; }
From source file:com.machinepublishers.jbrowserdriver.OptionsServer.java
License:Apache License
private static Cookie convert(org.apache.http.cookie.Cookie in) { return new Cookie(in.getName(), in.getValue(), in.getDomain(), in.getPath(), in.getExpiryDate(), in.isSecure());/* ww w. j a v a2 s.co m*/ }
From source file:com.machinepublishers.jbrowserdriver.OptionsServer.java
License:Apache License
/** * {@inheritDoc}//from w w w .ja v a2 s . c o m */ @Override public void deleteCookie(Cookie cookie) { List<org.apache.http.cookie.Cookie> cookies = cookieStore.getCookies(); String toDelete = new StringBuilder().append(cookie.getDomain().toLowerCase()).append("\n") .append(cookie.getName().toLowerCase()).append("\n").append(cookie.getPath().toLowerCase()) .toString(); for (org.apache.http.cookie.Cookie cur : cookies) { String curString = new StringBuilder().append(cur.getDomain().toLowerCase()).append("\n") .append(cur.getName().toLowerCase()).append("\n").append(cur.getPath().toLowerCase()) .toString(); if (toDelete.equals(curString)) { removeFromCookieStore(cur); } } }