Instantly share code, notes, and snippets.
Created
November 21, 2018 15:17
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save ckzgraphics/96e9d82e48e84b8030e4bd6e1eeb16cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.net.URL; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.Set; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.Cookie; | |
| import org.openqa.selenium.JavascriptExecutor; | |
| import org.openqa.selenium.OutputType; | |
| import org.openqa.selenium.TakesScreenshot; | |
| import org.openqa.selenium.WebDriver; | |
| import org.openqa.selenium.chrome.ChromeDriver; | |
| import org.openqa.selenium.chrome.ChromeOptions; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| public class USER_Test_Cookies_Chrome { | |
| public static void main(String[] args) { | |
| String USERNAME = "<USERNAME>"; | |
| String AUTOMATE_KEY = "<ACCESS_KEY>"; | |
| String HUB_URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; | |
| WebDriver webDriver = null; | |
| String AUT_URL = "chrome://settings/content/cookies"; | |
| // System.setProperty("webdriver.chrome.driver", "/Users/chimi/stack/drv/chromedriver"); | |
| URL URLObj = null; | |
| DesiredCapabilities caps = null; | |
| JavascriptExecutor js = null; | |
| Cookie cookie = null; | |
| try { | |
| URLObj = new URL(HUB_URL); | |
| caps = new DesiredCapabilities(); | |
| ChromeOptions options = new ChromeOptions(); | |
| Map<String, Object> prefs = new HashMap<>(); | |
| prefs.put("profile.default_content_setting_values.cookies", 1); // 1- ENABLED | 2 - DISABLED | |
| options.setExperimentalOption("prefs", prefs); | |
| caps.setCapability(ChromeOptions.CAPABILITY,options); | |
| caps.setCapability("browser", "Chrome"); | |
| caps.setCapability("browser_version", "70.0"); | |
| caps.setCapability("os", "OS X"); | |
| caps.setCapability("os_version", "High Sierra"); | |
| caps.setCapability("project", "Test Run"); | |
| caps.setCapability("build", "Support Automate"); | |
| caps.setCapability("name", "Test: Add/Delete Cookies"); | |
| caps.setCapability("browserstack.use_w3c", "true"); | |
| caps.setCapability("browserstack.console", "verbose"); | |
| caps.setCapability("browserstack.debug", "true"); | |
| caps.setCapability("browserstack.video", "true"); | |
| caps.setCapability("browserstack.selenium_version", "3.14.0"); | |
| webDriver = new RemoteWebDriver(URLObj, caps); | |
| // webDriver = new ChromeDriver(options); | |
| js = (JavascriptExecutor) webDriver; | |
| webDriver.manage().window().maximize(); | |
| webDriver.get(AUT_URL); | |
| Thread.sleep(4000); | |
| take_screenshot(webDriver); | |
| AUT_URL= "https://www.whatismybrowser.com/detect/are-cookies-enabled"; | |
| webDriver.get(AUT_URL); | |
| Thread.sleep(4000); | |
| take_screenshot(webDriver); | |
| AUT_URL = "https://it.altervista.org/registrazione.php?action=form&atype=blog"; | |
| webDriver.get(AUT_URL); | |
| Thread.sleep(4000); | |
| webDriver.findElement(By.id("facebook-connect")).click(); | |
| Thread.sleep(4000); | |
| Set<String> windwos = webDriver.getWindowHandles(); | |
| String oldWindow = webDriver.getWindowHandle(); | |
| System.out.println("oldWindow :: " + oldWindow); | |
| String newWindow = null; | |
| for (String string : windwos) { | |
| System.out.println("Wind :: " + string); | |
| newWindow = string; | |
| } | |
| webDriver.switchTo().window(newWindow); | |
| Thread.sleep(4000); | |
| System.out.println(webDriver.getTitle()); | |
| webDriver.findElement(By.name("email")).sendKeys("<FACEBOOK_LOGIN_EMAIL>"); | |
| webDriver.findElement(By.name("pass")).sendKeys("<FACEBOOK_LOGIN_PWD>"); | |
| webDriver.findElement(By.name("pass")).submit(); | |
| take_screenshot(webDriver); | |
| Thread.sleep(10000); | |
| webDriver.switchTo().window(oldWindow); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } finally { | |
| if(webDriver != null){ | |
| webDriver.quit(); | |
| } | |
| } // TRY CATCH END | |
| } // MAIN END | |
| public static void take_screenshot(WebDriver webDriver){ | |
| try { | |
| TakesScreenshot scrShot =((TakesScreenshot)webDriver); | |
| File SrcFile=scrShot.getScreenshotAs(OutputType.FILE); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } // FUNC END | |
| } // CLASS END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment