List of usage examples for org.apache.http.client CookieStore addCookie
void addCookie(Cookie cookie);
From source file:edu.mit.scratch.ScratchProject.java
public void setLoved(final ScratchSession session, final boolean loved) throws ScratchProjectException { final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build(); final CookieStore cookieStore = new BasicCookieStore(); final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en"); final BasicClientCookie sessid = new BasicClientCookie("scratchsessionsid", session.getSessionID()); final BasicClientCookie token = new BasicClientCookie("scratchcsrftoken", session.getCSRFToken()); final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true"); lang.setDomain(".scratch.mit.edu"); lang.setPath("/"); sessid.setDomain(".scratch.mit.edu"); sessid.setPath("/"); token.setDomain(".scratch.mit.edu"); token.setPath("/"); debug.setDomain(".scratch.mit.edu"); debug.setPath("/"); cookieStore.addCookie(lang); cookieStore.addCookie(sessid);//from ww w . j ava2s . co m cookieStore.addCookie(token); cookieStore.addCookie(debug); final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig) .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64)" + " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/" + "537.36") .setDefaultCookieStore(cookieStore).build(); CloseableHttpResponse resp; final HttpUriRequest update = RequestBuilder.put() .setUri("https://scratch.mit.edu/site-api/users/lovers/" + this.getProjectID() + "/" + (loved ? "add" : "remove") + "/?usernames=" + session.getUsername()) .addHeader("Accept", "application/json, text/javascript, */*; q=0.01").addHeader("DNT", "1") .addHeader("Referer", "https://scratch.mit.edu/projects/" + this.getProjectID() + "/") .addHeader("Origin", "https://scratch.mit.edu/").addHeader("Accept-Encoding", "gzip, deflate, sdch") .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json") .addHeader("X-Requested-With", "XMLHttpRequest").addHeader("Cookie", "scratchsessionsid=" + session.getSessionID() + "; scratchcsrftoken=" + session.getCSRFToken()) .addHeader("X-CSRFToken", session.getCSRFToken()).build(); try { resp = httpClient.execute(update); if (resp.getStatusLine().getStatusCode() != 200) throw new ScratchProjectException(); final BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent())); final StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) result.append(line); } catch (final IOException e) { e.printStackTrace(); throw new ScratchProjectException(); } BufferedReader rd; try { rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent())); } catch (UnsupportedOperationException | IOException e) { e.printStackTrace(); throw new ScratchProjectException(); } }
From source file:edu.mit.scratch.ScratchProject.java
public boolean comment(final ScratchSession session, final String comment) throws ScratchProjectException { final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build(); final CookieStore cookieStore = new BasicCookieStore(); final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en"); final BasicClientCookie sessid = new BasicClientCookie("scratchsessionsid", session.getSessionID()); final BasicClientCookie token = new BasicClientCookie("scratchcsrftoken", session.getCSRFToken()); final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true"); lang.setDomain(".scratch.mit.edu"); lang.setPath("/"); sessid.setDomain(".scratch.mit.edu"); sessid.setPath("/"); token.setDomain(".scratch.mit.edu"); token.setPath("/"); debug.setDomain(".scratch.mit.edu"); debug.setPath("/"); cookieStore.addCookie(lang); cookieStore.addCookie(sessid);/* ww w . j a v a2 s . com*/ cookieStore.addCookie(token); cookieStore.addCookie(debug); final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig) .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64)" + " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/" + "537.36") .setDefaultCookieStore(cookieStore).build(); CloseableHttpResponse resp; final JSONObject obj = new JSONObject(); obj.put("content", comment); obj.put("parent_id", ""); obj.put("commentee_id", ""); final String strData = obj.toString(); HttpUriRequest update = null; try { update = RequestBuilder.post() .setUri("https://scratch.mit.edu/site-api/comments/project/" + this.getProjectID() + "/add/") .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") .addHeader("Referer", "https://scratch.mit.edu/projects/" + this.getProjectID() + "/") .addHeader("Origin", "https://scratch.mit.edu/") .addHeader("Accept-Encoding", "gzip, deflate, sdch") .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json") .addHeader("X-Requested-With", "XMLHttpRequest") .addHeader("Cookie", "scratchsessionsid=" + session.getSessionID() + "; scratchcsrftoken=" + session.getCSRFToken()) .addHeader("X-CSRFToken", session.getCSRFToken()).setEntity(new StringEntity(strData)).build(); } catch (final UnsupportedEncodingException e1) { e1.printStackTrace(); } try { resp = httpClient.execute(update); System.out.println("cmntadd:" + resp.getStatusLine()); final BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent())); final StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) result.append(line); System.out.println("cmtline:" + result.toString()); } catch (final IOException e) { e.printStackTrace(); throw new ScratchProjectException(); } BufferedReader rd; try { rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent())); } catch (UnsupportedOperationException | IOException e) { e.printStackTrace(); throw new ScratchProjectException(); } return false; }
From source file:fr.eolya.utils.http.HttpLoader.java
/** * @param /* w ww. j a v a2 s . c o m*/ * @return */ private HttpClient getHttpClient(String url) { try { // ClientConnectionManager SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); registry.register(new Scheme("https", 443, sf)); ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); // Params HttpParams httpParams = getHttpParams(); // DefaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(ccm, httpParams); // Proxy setProxy(httpClient, url, proxyHost, proxyPort, proxyExclude, proxyUserName, proxyPassword); // if (StringUtils.isNotEmpty(proxyHost)) { // if (StringUtils.isNotEmpty(proxyUserName) && StringUtils.isNotEmpty(proxyPassword)) { // httpClient.getCredentialsProvider().setCredentials( // new AuthScope(proxyHost,Integer.valueOf(proxyPort)), // new UsernamePasswordCredentials(proxyUserName, proxyPassword)); // } // HttpHost proxy = new HttpHost(proxyHost,Integer.valueOf(proxyPort)); // httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); // } else { // httpClient.getParams().removeParameter(ConnRoutePNames.DEFAULT_PROXY); // } // Cookies if (cookies != null) { CookieStore cookieStore = new BasicCookieStore(); Iterator<Entry<String, String>> it = cookies.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next(); BasicClientCookie cookie = new BasicClientCookie(pairs.getKey(), pairs.getValue()); //cookie.setDomain("your domain"); cookie.setPath("/"); cookieStore.addCookie(cookie); } httpClient.setCookieStore(cookieStore); } return new DecompressingHttpClient(httpClient); } catch (Exception e) { return new DecompressingHttpClient(new DefaultHttpClient()); } }
From source file:org.geometerplus.android.fbreader.network.NetworkLibraryActivity.java
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Connection.bindToService(this, new Runnable() { public void run() { getListView().invalidateViews(); }/* w w w . j a va 2 s . c o m*/ }); if (resultCode != RESULT_OK || data == null) { return; } switch (requestCode) { case REQUEST_MANAGE_CATALOGS: { final ArrayList<String> myIds = data.getStringArrayListExtra(ENABLED_CATALOG_IDS_KEY); NetworkLibrary.Instance().setActiveIds(myIds); NetworkLibrary.Instance().synchronize(); break; } case REQUEST_AUTHORISATION_SCREEN: { final CookieStore store = ZLNetworkManager.Instance().cookieStore(); final Map<String, String> cookies = (Map<String, String>) data.getSerializableExtra(COOKIES_KEY); if (cookies == null) { break; } for (Map.Entry<String, String> entry : cookies.entrySet()) { final BasicClientCookie2 c = new BasicClientCookie2(entry.getKey(), entry.getValue()); c.setDomain(data.getData().getHost()); c.setPath("/"); final Calendar expire = Calendar.getInstance(); expire.add(Calendar.YEAR, 1); c.setExpiryDate(expire.getTime()); c.setSecure(true); c.setDiscard(false); store.addCookie(c); } final NetworkTree tree = getTreeByKey((FBTree.Key) data.getSerializableExtra(TREE_KEY_KEY)); new ReloadCatalogAction(this).run(tree); break; } } }
From source file:edu.mit.scratch.ScratchUser.java
public boolean comment(final ScratchSession session, final String comment) throws ScratchProjectException { final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build(); final CookieStore cookieStore = new BasicCookieStore(); final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en"); final BasicClientCookie sessid = new BasicClientCookie("scratchsessionsid", session.getSessionID()); final BasicClientCookie token = new BasicClientCookie("scratchcsrftoken", session.getCSRFToken()); final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true"); lang.setDomain(".scratch.mit.edu"); lang.setPath("/"); sessid.setDomain(".scratch.mit.edu"); sessid.setPath("/"); token.setDomain(".scratch.mit.edu"); token.setPath("/"); debug.setDomain(".scratch.mit.edu"); debug.setPath("/"); cookieStore.addCookie(lang); cookieStore.addCookie(sessid);//from w w w. j av a2s .co m cookieStore.addCookie(token); cookieStore.addCookie(debug); final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig) .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build(); CloseableHttpResponse resp; final JSONObject obj = new JSONObject(); obj.put("content", comment); obj.put("parent_id", ""); obj.put("commentee_id", ""); final String strData = obj.toString(); HttpUriRequest update = null; try { update = RequestBuilder.post() .setUri("https://scratch.mit.edu/site-api/comments/user/" + this.getUsername() + "/add/") .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") .addHeader("Referer", "https://scratch.mit.edu/users/" + this.getUsername() + "/") .addHeader("Origin", "https://scratch.mit.edu/") .addHeader("Accept-Encoding", "gzip, deflate, sdch") .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json") .addHeader("X-Requested-With", "XMLHttpRequest") .addHeader("Cookie", "scratchsessionsid=" + session.getSessionID() + "; scratchcsrftoken=" + session.getCSRFToken()) .addHeader("X-CSRFToken", session.getCSRFToken()).setEntity(new StringEntity(strData)).build(); } catch (final UnsupportedEncodingException e1) { e1.printStackTrace(); } try { resp = httpClient.execute(update); final BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent())); final StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) result.append(line); } catch (final IOException e) { e.printStackTrace(); throw new ScratchProjectException(); } return false; }
From source file:com.nesscomputing.httpclient.factory.httpclient4.ApacheHttpClient4Factory.java
private <T> void contributeCookies(final DefaultHttpClient httpClient, final HttpClientRequest<T> httpClientRequest) { final List<Cookie> cookies = httpClientRequest.getCookies(); if (CollectionUtils.isNotEmpty(cookies)) { final CookieStore cookieStore = new BasicCookieStore(); for (final Cookie cookie : cookies) { final BasicClientCookie httpCookie = new BasicClientCookie(cookie.getName(), cookie.getValue()); final int maxAge = cookie.getMaxAge(); if (maxAge > 0) { final Date expire = new Date(System.currentTimeMillis() + maxAge * 1000L); httpCookie.setExpiryDate(expire); httpCookie.setAttribute(ClientCookie.MAX_AGE_ATTR, Integer.toString(maxAge)); }//from w w w . j av a2 s . com httpCookie.setVersion(1); httpCookie.setPath(cookie.getPath()); httpCookie.setDomain(cookie.getDomain()); httpCookie.setSecure(cookie.getSecure()); LOG.debug("Adding cookie to the request: '%s'", httpCookie); cookieStore.addCookie(httpCookie); } httpClient.setCookieStore(cookieStore); } else { LOG.debug("No cookies found."); httpClient.setCookieStore(null); } }
From source file:me.vertretungsplan.parser.LoginHandler.java
private String handleLogin(Executor executor, CookieStore cookieStore, boolean needsResponse) throws JSONException, IOException, CredentialInvalidException { if (auth == null) return null; if (!(auth instanceof UserPasswordCredential || auth instanceof PasswordCredential)) { throw new IllegalArgumentException("Wrong authentication type"); }//from ww w . j a va 2 s . co m String login; String password; if (auth instanceof UserPasswordCredential) { login = ((UserPasswordCredential) auth).getUsername(); password = ((UserPasswordCredential) auth).getPassword(); } else { login = null; password = ((PasswordCredential) auth).getPassword(); } JSONObject data = scheduleData.getData(); JSONObject loginConfig = data.getJSONObject(LOGIN_CONFIG); String type = loginConfig.optString(PARAM_TYPE, "post"); switch (type) { case "post": List<Cookie> cookieList = cookieProvider != null ? cookieProvider.getCookies(auth) : null; if (cookieList != null && !needsResponse) { for (Cookie cookie : cookieList) cookieStore.addCookie(cookie); String checkUrl = loginConfig.optString(PARAM_CHECK_URL, null); String checkText = loginConfig.optString(PARAM_CHECK_TEXT, null); if (checkUrl != null && checkText != null) { String response = executor.execute(Request.Get(checkUrl)).returnContent().asString(); if (!response.contains(checkText)) { return null; } } else { return null; } } executor.clearCookies(); Document preDoc = null; if (loginConfig.has(PARAM_PRE_URL)) { String preUrl = loginConfig.getString(PARAM_PRE_URL); String preHtml = executor.execute(Request.Get(preUrl)).returnContent().asString(); preDoc = Jsoup.parse(preHtml); } String postUrl = loginConfig.getString(PARAM_URL); JSONObject loginData = loginConfig.getJSONObject(PARAM_DATA); List<NameValuePair> nvps = new ArrayList<>(); String typo3Challenge = null; if (loginData.has("_hiddeninputs") && preDoc != null) { for (Element hidden : preDoc.select(loginData.getString("_hiddeninputs") + " input[type=hidden]")) { nvps.add(new BasicNameValuePair(hidden.attr("name"), hidden.attr("value"))); if (hidden.attr("name").equals("challenge")) { typo3Challenge = hidden.attr("value"); } } } for (String name : JSONObject.getNames(loginData)) { String value = loginData.getString(name); if (name.equals("_hiddeninputs")) continue; switch (value) { case "_login": value = login; break; case "_password": value = password; break; case "_password_md5": value = DigestUtils.md5Hex(password); break; case "_password_md5_typo3": value = DigestUtils.md5Hex(login + ":" + DigestUtils.md5Hex(password) + ":" + typo3Challenge); break; } nvps.add(new BasicNameValuePair(name, value)); } Request request = Request.Post(postUrl); if (loginConfig.optBoolean("form-data", false)) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (NameValuePair nvp : nvps) { builder.addTextBody(nvp.getName(), nvp.getValue()); } request.body(builder.build()); } else { request.bodyForm(nvps, Charset.forName("UTF-8")); } String html = executor.execute(request).returnContent().asString(); if (cookieProvider != null) cookieProvider.saveCookies(auth, cookieStore.getCookies()); String checkUrl = loginConfig.optString(PARAM_CHECK_URL, null); String checkText = loginConfig.optString(PARAM_CHECK_TEXT, null); if (checkUrl != null && checkText != null) { String response = executor.execute(Request.Get(checkUrl)).returnContent().asString(); if (response.contains(checkText)) throw new CredentialInvalidException(); } else if (checkText != null) { if (html.contains(checkText)) throw new CredentialInvalidException(); } return html; case "basic": if (login == null) throw new IOException("wrong auth type"); executor.auth(login, password); if (loginConfig.has(PARAM_URL)) { String url = loginConfig.getString(PARAM_URL); if (executor.execute(Request.Get(url)).returnResponse().getStatusLine().getStatusCode() != 200) { throw new CredentialInvalidException(); } } break; case "ntlm": if (login == null) throw new IOException("wrong auth type"); executor.auth(login, password, null, null); if (loginConfig.has(PARAM_URL)) { String url = loginConfig.getString(PARAM_URL); if (executor.execute(Request.Get(url)).returnResponse().getStatusLine().getStatusCode() != 200) { throw new CredentialInvalidException(); } } break; case "fixed": String loginFixed = loginConfig.optString(PARAM_LOGIN, null); String passwordFixed = loginConfig.getString(PARAM_PASSWORD); if (!Objects.equals(loginFixed, login) || !Objects.equals(passwordFixed, password)) { throw new CredentialInvalidException(); } break; } return null; }
From source file:net.emphased.vkclient.VkClient.java
/** * Login to Vk site./*w w w.j a v a2 s . co m*/ * @param email email for login. * @param password password for login. * @return API instance. * @throws VkException if login failed. */ public VkApi login(String email, String password) throws IOException, VkException { CookieStore cookieStore = getCookieStore(email); HttpContext httpContext = getHttpContext(cookieStore); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("noredirect", "1")); params.add(new BasicNameValuePair("email", email)); params.add(new BasicNameValuePair("pass", password)); HttpPost post = new HttpPost(DEFAULT_LOGIN_URL); post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); post.addHeader("X-Requested-With", "XMLHttpRequest"); HttpResponse resp = _httpClient.execute(post, httpContext); String respContent = EntityUtils.toString(resp.getEntity()); VkLoginResult loginResult = _objectMapper.readValue(respContent, VkLoginResult.class); if (!StringUtils.isEmpty(loginResult.getErrorMessage())) { throw new VkException(loginResult.getErrorMessage()); } Cookie sessionCookie = CookieUtils.findCookie(cookieStore, SESSION_COOKIE); if (sessionCookie == null) { throw new VkException("No session cookie found"); } // Store LoginResult for future cookie-based logins. BasicClientCookie sessionInfoCookie = new BasicClientCookie(SESSION_INFO_COOKIE, respContent); sessionInfoCookie.setExpiryDate(sessionCookie.getExpiryDate()); cookieStore.addCookie(sessionInfoCookie); return createApi(httpContext, loginResult); }
From source file:webtest.Test1.java
public void runTest1() { List<String> ouList = new ArrayList<>(); int count = 0; try {//www.j ava 2 s . c o m BufferedReader in; in = new BufferedReader( new FileReader("C:\\Users\\hallm8\\Documents\\NetBeansProjects\\WebTest\\src\\webtest\\OU")); String str; while ((str = in.readLine()) != null) { ouList.add(str); } } catch (FileNotFoundException ex) { Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex); } FirefoxProfile fp = new FirefoxProfile( new File("C:\\Users\\hallm8\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\tiu8eb0h.default")); fp.setPreference("webdriver.load.strategy", "unstable"); WebDriver driver = new FirefoxDriver(fp); driver.manage().window().maximize(); driver.get("http://byui.brightspace.com/d2l/login?noredirect=true"); WebElement myDynamicElement = (new WebDriverWait(driver, 60)) .until(ExpectedConditions.presenceOfElementLocated(By.id("d2l_minibar_placeholder"))); // times out after 60 seconds Actions actions = new Actions(driver); for (String ouList1 : ouList) { WebDriverWait wait = new WebDriverWait(driver, 60); /** * PULLING VALENCE REQUESTS * * Step 1: Open up Selenium and authenticate by having the user sign * in. This bypasses the Authorization Protection * * Step 2: Open up HTTP Client and pass the cookies into it. * * Step 3: Open up the JSON parser of your choosing and parse into * it! */ try { Set<Cookie> seleniumCookies = driver.manage().getCookies(); CookieStore cookieStore = new BasicCookieStore(); for (Cookie seleniumCookie : seleniumCookies) { BasicClientCookie basicClientCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); basicClientCookie.setDomain(seleniumCookie.getDomain()); basicClientCookie.setExpiryDate(seleniumCookie.getExpiry()); basicClientCookie.setPath(seleniumCookie.getPath()); cookieStore.addCookie(basicClientCookie); } HttpClient httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); HttpGet request = new HttpGet( "https://byui.brightspace.com/d2l/api/le/1.7/" + ouList1 + "/content/toc"); request.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); String jsonString = EntityUtils.toString(response.getEntity()); JSONObject obj = new JSONObject(jsonString); JSONArray modules = obj.getJSONArray("Modules"); System.out.println(jsonString); for (int i = 0; i < modules.length(); i++) { if (modules.getJSONObject(i).has("Modules")) { modules.put(modules.getJSONObject(i).getJSONArray("Modules")); } System.out.println(modules.get(i)); JSONArray topics = modules.getJSONObject(i).getJSONArray("Topics"); for (int j = 0; j < topics.length(); j++) { JSONObject topic = topics.getJSONObject(j); System.out.println(topic.get("Title")); } } JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); /** * This covers Dropbox Folders * * System.out.println(ouList1); * driver.get("https://byui.brightspace.com/d2l/lms/dropbox/admin/folders_manage.d2l?ou=" * + ouList1); driver.manage().timeouts().implicitlyWait(3, * TimeUnit.SECONDS); * * List<WebElement> links = * driver.findElements(By.xpath("//a[contains(@href, * '/d2l/lms/dropbox/admin/mark/')]")); * * ArrayList<String> dropBoxes = new ArrayList<>(); * * System.out.println(links.size()); for (WebElement link : * links) { * * System.out.println(link.getAttribute("href")); if * (link.getAttribute("href") != null && * link.getAttribute("href").contains("/d2l/lms/dropbox/admin/mark/")) * { * dropBoxes.add(link.getAttribute("href").replace("mark/folder_submissions_users", * "modify/folder_newedit_properties")); * System.out.println("successfully pulled: " + * link.getAttribute("href")); } } * * for (int j = 0; j < dropBoxes.size(); j++) { String dropBox = * dropBoxes.get(j); driver.get(dropBox); * * if (!driver.findElements(By.linkText("Show Submission * Options")).isEmpty()) { driver.findElement(By.linkText("Show * Submission Options")).click(); * driver.manage().timeouts().implicitlyWait(1800, * TimeUnit.SECONDS); } * * if (driver.findElement(By.id("z_cd")).isSelected()) { * //((JavascriptExecutor) * driver).executeScript("arguments[0].scrollIntoView(true);", * driver.findElement(By.id("z_ce"))); * actions.moveToElement(driver.findElement(By.id("z_ce"))).click().perform(); * actions.moveToElement(driver.findElement(By.id("z_ci"))).click().perform(); * driver.findElement(By.id("z_c")).click(); * driver.manage().timeouts().implicitlyWait(3, * TimeUnit.SECONDS); } } * * // Response response = json.fromJson(, Response.class) /** * This covers content. */ /* driver.get("https://byui.brightspace.com/d2l/le/content/9730/Home"); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); List<WebElement> dragElement = driver.findElements(By.xpath("//div[contains(@id,'TreeItem')]//div[contains(@class, 'd2l-textblock')]")); /* for (int i = 4; i < dragElement.size(); i++) { WebElement drag = dragElement.get(i); drag.click(); wait.until(ExpectedConditions.elementToBeClickable(drag)); /* driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); System.out.println(driver.findElement(By.xpath("//h1[contains(@class, 'd2l-page-title d2l-heading vui-heading-1')]")).getText()); (new WebDriverWait(driver, 60)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return drag.getText().contains(d.findElement(By.xpath("//h1[contains(@class, 'd2l-page-title d2l-heading vui-heading-1')]")).getText()); } }); try { // while the following loop runs, the DOM changes - // page is refreshed, or element is removed and re-added // This took me forever to figure out!!! Thread.sleep(2000); } catch (InterruptedException ex) { Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex); } List<WebElement> contentItems = driver.findElements(By.className("d2l-fuzzydate")); for (int k = 1; k < contentItems.size(); k++) { WebElement content = contentItems.get(k); wait.until(presenceOfElementLocated(By.className(content.getAttribute("class")))); WebElement parent1 = content.findElement(By.xpath("..")); System.out.println(parent1.getTagName()); WebElement parent2 = parent1.findElement(By.xpath("..")); System.out.println(parent2.getTagName()); WebElement parent3 = parent2.findElement(By.xpath("..")); System.out.println(parent3.getTagName()); WebElement parent4 = parent3.findElement(By.xpath("..")); System.out.println(parent4.getTagName()); WebElement parent5 = parent4.findElement(By.xpath("..")); System.out.println(parent5.getTagName()); WebElement parent6 = parent5.findElement(By.xpath("..")); System.out.println(parent6.getTagName()); //System.out.println(parent5.getText()); System.out.println(parent6.getAttribute("title")); } } */ /** * This covers quizzes */ /* driver.get("https://byui.brightspace.com/d2l/lms/quizzing/admin/quizzes_manage.d2l?ou=" + ouList1); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); List<WebElement> links = driver.findElements(By.className("vui-outline")); ArrayList<String> quizzes = new ArrayList<>(); for (WebElement link : links) { if (link.getAttribute("href") != null && link.getAttribute("href").contains("byui.brightspace.com/d2l/lms/quizzing/admin/modify")) { quizzes.add(link.getAttribute("href")); System.out.println("successfully pulled: " + link.getAttribute("href")); } } for (int j = 0; j < quizzes.size(); j++) { String quiz = quizzes.get(j); boolean isLA = false; driver.get(quiz); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); if (!driver.findElements(By.linkText("Expand optional advanced properties")).isEmpty()) { driver.findElement(By.linkText("Expand optional advanced properties")).click(); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); } if (driver.findElement(By.name("disableRightClick")).isSelected()) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", driver.findElement(By.name("disableRightClick"))); driver.findElement(By.name("disableRightClick")).click(); driver.findElement(By.id("z_b")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); count++; } List<WebElement> labels = driver.findElements(By.tagName("label")); for (WebElement label : labels) { if (label.getText().contains("LA")) { isLA = true; break; } } driver.findElement(By.id("z_h_Assessment_l")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); if (driver.findElement(By.name("autoExportGrades")).isSelected() && isLA == true) { driver.findElement(By.name("autoExportGrades")).click(); count++; } if (driver.findElement(By.name("autoSetGraded")).isSelected() && isLA == true) { driver.findElement(By.name("autoSetGraded")).click(); count++; } if (!driver.findElement(By.name("autoSetGraded")).isSelected() && isLA == false) { driver.findElement(By.name("autoSetGraded")).click(); count++; } if (!driver.findElement(By.name("autoExportGrades")).isSelected() && isLA == false) { driver.findElement(By.name("autoExportGrades")).click(); count++; } driver.findElement(By.id("z_b")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); System.out.println("count is: " + count); /** * * Submission Views * */ /* driver.findElement(By.linkText("Submission Views")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); driver.findElement(By.linkText("Default View")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); if (!driver.findElement(By.name("showQuestions")).isSelected()) { System.out.println("show answers clicked!!! URL: " + quiz); driver.findElement(By.name("showQuestions")).click(); } if (!driver.findElement(By.id("z_p")).isSelected()) { driver.findElement(By.id("z_p")).click(); } if (!driver.findElement(By.name("showCorrectAnswers")).isSelected()) { driver.findElement(By.name("showCorrectAnswers")).click(); } if (!driver.findElement(By.name("showQuestionScore")).isSelected()) { driver.findElement(By.name("showQuestionScore")).click(); } if (!driver.findElement(By.name("showScore")).isSelected()) { driver.findElement(By.name("showScore")).click(); } driver.findElement(By.id("z_a")).click(); */ //} /** * This covers content. */ /* driver.get("https://byui.brightspace.com/d2l/le/content/9730/Home"); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); List<WebElement> dragElement = driver.findElements(By.xpath("//div[contains(@id,'TreeItem')]//div[contains(@class, 'd2l-textblock')]")); /* for (int i = 4; i < dragElement.size(); i++) { WebElement drag = dragElement.get(i); drag.click(); wait.until(ExpectedConditions.elementToBeClickable(drag)); /* driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); System.out.println(driver.findElement(By.xpath("//h1[contains(@class, 'd2l-page-title d2l-heading vui-heading-1')]")).getText()); (new WebDriverWait(driver, 60)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return drag.getText().contains(d.findElement(By.xpath("//h1[contains(@class, 'd2l-page-title d2l-heading vui-heading-1')]")).getText()); } }); try { // while the following loop runs, the DOM changes - // page is refreshed, or element is removed and re-added // This took me forever to figure out!!! Thread.sleep(2000); } catch (InterruptedException ex) { Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex); } List<WebElement> contentItems = driver.findElements(By.className("d2l-fuzzydate")); for (int k = 1; k < contentItems.size(); k++) { WebElement content = contentItems.get(k); wait.until(presenceOfElementLocated(By.className(content.getAttribute("class")))); WebElement parent1 = content.findElement(By.xpath("..")); System.out.println(parent1.getTagName()); WebElement parent2 = parent1.findElement(By.xpath("..")); System.out.println(parent2.getTagName()); WebElement parent3 = parent2.findElement(By.xpath("..")); System.out.println(parent3.getTagName()); WebElement parent4 = parent3.findElement(By.xpath("..")); System.out.println(parent4.getTagName()); WebElement parent5 = parent4.findElement(By.xpath("..")); System.out.println(parent5.getTagName()); WebElement parent6 = parent5.findElement(By.xpath("..")); System.out.println(parent6.getTagName()); //System.out.println(parent5.getText()); System.out.println(parent6.getAttribute("title")); } } */ /** * This covers quizzes */ /* driver.get("https://byui.brightspace.com/d2l/lms/quizzing/admin/quizzes_manage.d2l?ou=" + ouList1); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); System.out.println("Opening OU# " + ouList1); wait.until(ExpectedConditions.elementToBeClickable(By.className("d2l-tool-areas"))); List<WebElement> links = driver.findElements(By.xpath("//a[contains(@href,'/d2l/lms/quizzing/admin/modify/quiz_newedit_properties.d2l?qi=')]")); System.out.println("viu outline obtained"); ArrayList<String> quizzes = new ArrayList<>(); System.out.println(links.size()); for (WebElement link : links) { if (link.getAttribute("href") != null && link.getAttribute("href").contains("byui.brightspace.com/d2l/lms/quizzing/admin/modify")) { quizzes.add(link.getAttribute("href")); System.out.println("successfully pulled: " + link.getAttribute("href")); } } System.out.println(quizzes.size()); for (int j = 0; j < quizzes.size(); j++) { String quiz = quizzes.get(j); boolean isLA = false; driver.get(quiz); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); if (!driver.findElements(By.linkText("Expand optional advanced properties")).isEmpty()) { driver.findElement(By.linkText("Expand optional advanced properties")).click(); driver.manage().timeouts().implicitlyWait(1800, TimeUnit.SECONDS); } wait.until(ExpectedConditions.elementToBeClickable(By.name("disableRightClick"))); if (driver.findElement(By.name("disableRightClick")).isSelected()) { driver.findElement(By.name("disableRightClick")).click(); driver.findElement(By.id("z_b")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); count++; } List<WebElement> longAnswer = driver.findElements(By.xpath("//label[contains(.,'LA')]")); if (longAnswer.size() > 0) { isLA = true; } quiz = quiz.replace("/quiz_newedit_properties", "/quiz_newedit_assessment"); driver.get(quiz); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.name("autoExportGrades"))); if (driver.findElement(By.name("autoExportGrades")).isSelected() && isLA == true) { driver.findElement(By.name("autoExportGrades")).click(); count++; } wait.until(ExpectedConditions.elementToBeClickable(By.name("autoSetGraded"))); if (driver.findElement(By.name("autoSetGraded")).isSelected() && isLA == true) { driver.findElement(By.name("autoSetGraded")).click(); count++; } wait.until(ExpectedConditions.elementToBeClickable(By.name("autoSetGraded"))); if (!driver.findElement(By.name("autoSetGraded")).isSelected() && isLA == false) { driver.findElement(By.name("autoSetGraded")).click(); count++; } wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.name("autoExportGrades"))); if (!driver.findElement(By.name("autoExportGrades")).isSelected() && isLA == false) { driver.findElement(By.name("autoExportGrades")).click(); count++; } wait.until(ExpectedConditions.elementToBeClickable(By.id("z_b"))); driver.findElement(By.id("z_b")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); System.out.println("count is: " + count); /** * * Submission Views * */ /* driver.findElement(By.linkText("Submission Views")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); driver.findElement(By.linkText("Default View")).click(); driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS); if (!driver.findElement(By.name("showQuestions")).isSelected()) { System.out.println("show answers clicked!!! URL: " + quiz); driver.findElement(By.name("showQuestions")).click(); } if (!driver.findElement(By.id("z_p")).isSelected()) { driver.findElement(By.id("z_p")).click(); } if (!driver.findElement(By.name("showCorrectAnswers")).isSelected()) { driver.findElement(By.name("showCorrectAnswers")).click(); } if (!driver.findElement(By.name("showQuestionScore")).isSelected()) { driver.findElement(By.name("showQuestionScore")).click(); } if (!driver.findElement(By.name("showScore")).isSelected()) { driver.findElement(By.name("showScore")).click(); } driver.findElement(By.id("z_a")).click(); */ //} /** * End of FOR LOOP stub */ } catch (IOException ex) { Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex); } } }