How to Work with browser cookies – getCookies, deleteCookieNamed, and deleteAllCookies using selenium webdriver?
problem?
How to Work with browser cookies – getCookies, deleteCookieNamed, and deleteAllCookies using selenium webdriver?
Solution: 🙂
import java.util.Iterator; import java.util.Set; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; public class CookieManagment { public static void main(String[] args) { ProfilesIni allProfiles = new ProfilesIni(); FirefoxProfile myProfile = allProfiles.getProfile("MyTestProfile"); myProfile.setPreference("capability.policy.default.Window.frameElement.get","allAccess"); WebDriver myTestDriver = new FirefoxDriver(myProfile); myTestDriver.manage().window().maximize(); myTestDriver.get("http://linkedin.com"); //getCookies() - Get all the cookies for the current domain. Set<Cookie> testCookie = myTestDriver.manage().getCookies(); Iterator<Cookie> iter= testCookie.iterator(); while(iter.hasNext()){ Cookie C = iter.next(); System.out.println(C.getName()+"-------------------" + C.getPath()+"--------------------"+ C.getDomain()+"----"+C.getValue()+"---"+C.getExpiry()); } //deleteCookieNamed(java.lang.String name)- Delete the named cookie from the current domain. myTestDriver.manage().deleteCookieNamed("JSESSIONID"); System.out.println("------------------------------------------------------------------------------------"); testCookie = myTestDriver.manage().getCookies(); iter= testCookie.iterator(); while(iter.hasNext()){ Cookie C = iter.next(); System.out.println(C.getName()+"-------------" + C.getPath()+"------------"+ C.getDomain()+"----"+C.getValue()+"---"+C.getExpiry()); } //deleteAllCookies() - Delete all the cookies for the current domain. myTestDriver.manage().deleteAllCookies(); testCookie = myTestDriver.manage().getCookies(); iter= testCookie.iterator(); while(iter.hasNext()){ Cookie C = iter.next(); System.out.println(C.getName()+"-------------" + C.getPath()+"------------"+ C.getDomain()+"----"+C.getValue()+"---"+C.getExpiry()); } } }
How to Handle untrusted SSL certificates using selenium webdriver?
What is the problem?
How to Handle untrusted SSL certificates using selenium webdriver?
Solution:
When this appears?
If the site has a security certificate, but it is wrong / outdated, then Firefox will warn you that someone might be trying to deceive you. You can bypass the message by clicking the “Add an exception” button, and then retrieving the certificate and viewing it.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; public class testHandleFireFoxcertificatesIssue { public static void main(String[] args) { //Class ProfilesIni details -> http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html ProfilesIni allProfiles = new ProfilesIni(); //Class FirefoxProfile -> http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html // Use FirefoxProfile Constructor FirefoxProfile myProfile = allProfiles.getProfile("CertificateIssue"); myProfile.setAcceptUntrustedCertificates(true); myProfile.setAssumeUntrustedCertificateIssuer(false); WebDriver myDriver = new FirefoxDriver(myProfile); myDriver.get("http://www.paypal.com"); } }
How Selenium webdriver use existing firefox profile ?
- Create object of Class ProfilesIni
- Call Method getProfile of Class ProfilesIni
- Which return a object of the type Class FirefoxProfile
- So Create new Class FirefoxProfile object and assign to it.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; public class testFirefoxProfile { public static void main(String[] args) { //Class ProfilesIni details -> http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html ProfilesIni allProfiles = new ProfilesIni(); //Class FirefoxProfile -> http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html // Use FirefoxProfile Constructor FirefoxProfile myProfile = allProfiles.getProfile("MyTestProfile"); WebDriver myDriver = new FirefoxDriver(myProfile); myDriver.get("http://twitter.com"); System.out.println("What is my URL --------------->" + myDriver.getCurrentUrl()); System.out.println("What is my Page title ----------->" + myDriver.getTitle()); System.out.println(""); myDriver.quit(); } }