Hướng dẫn selenium cheat sheet java

As you know, I am a big fan of Selenium WebDriver. You can find tonnes of useful Java code in my Web Automation Java Series. I lead automated testing courses and train people how to write tests all the time. The thing that was missing in the materials was a sheet containing all of the most relevant Java code snippets. So I decided to fill that gap. So, I created the most complete Selenium WebDriver Java cheat sheet. I hope that you will find it useful. Enjoy!

Initially, I created the C# cheat sheet while we developed the first versions of the BELLATRIX automated testing framework. Here is a Java version of it, revisited in 2021.

Basic Elements Operations

Advanced Elements Operations

Advanced Browser Operations

Advanced Browser Configurations

// Use a specific Firefox profile
ProfilesIni profile = new ProfilesIni[];
FirefoxProfile firefoxProfile = profile.getProfile["ProfileName"];
FirefoxOptions firefoxOptions = new FirefoxOptions[];
firefoxOptions.setProfile[firefoxProfile];
driver = new FirefoxDriver[firefoxOptions];
// Set a HTTP proxy Firefox
ProfilesIni profile = new ProfilesIni[];
FirefoxProfile firefoxProfile = new FirefoxProfile[];
firefoxProfile.setPreference["network.proxy.type", 1];
firefoxProfile.setPreference["network.proxy.http", "myproxy.com"];
firefoxProfile.setPreference["network.proxy.http_port", 3239];
FirefoxOptions firefoxOptions = new FirefoxOptions[];
firefoxOptions.setProfile[firefoxProfile];
driver = new FirefoxDriver[firefoxOptions];
// Set a HTTP proxy Chrome
var proxy = new Proxy[];
proxy.setProxyType[Proxy.ProxyType.MANUAL];
proxy.setAutodetect[false];
proxy.setSslProxy["127.0.0.1:3239"];
ChromeOptions chromeOptions = new ChromeOptions[];
chromeOptions.setProxy[proxy];
driver = new ChromeDriver[chromeOptions];
// Accept all certificates Firefox
FirefoxProfile firefoxProfile = new FirefoxProfile[];
firefoxProfile.setAcceptUntrustedCertificates[true];
firefoxProfile.setAssumeUntrustedCertificateIssuer[false];
FirefoxOptions firefoxOptions = new FirefoxOptions[];
firefoxOptions.setProfile[firefoxProfile];
driver = new FirefoxDriver[firefoxOptions];
// Accept all certificates Chrome
ChromeOptions chromeOptions = new ChromeOptions[];
chromeOptions.addArguments["--ignore-certificate-errors"];
driver = new ChromeDriver[chromeOptions];
// Set Chrome options
ChromeOptions chromeOptions = new ChromeOptions[];
chromeOptions.addArguments["user-data-dir=C:PathToUser Data"];
driver = new ChromeDriver[chromeOptions];
// Turn off the JavaScript Firefox
ProfilesIni profile = new ProfilesIni[];
FirefoxProfile firefoxProfile = profile.getProfile["ProfileName"];
firefoxProfile.setPreference["javascript.enabled", false];
FirefoxOptions firefoxOptions = new FirefoxOptions[];
firefoxOptions.setProfile[firefoxProfile];
driver = new FirefoxDriver[firefoxOptions];
// Set the default page load timeout
driver.manage[].timeouts[].pageLoadTimeout[10, TimeUnit.SECONDS];
// Start Firefox with plugins
FirefoxProfile profile = new FirefoxProfile[];
firefoxProfile.addExtension[new File["C:extensionsLocationextension.xpi"]];
FirefoxOptions firefoxOptions = new FirefoxOptions[];
firefoxOptions.setProfile[firefoxProfile];
driver = new FirefoxDriver[firefoxOptions];
// Start Chrome with an unpacked extension
ChromeOptions chromeOptions = new ChromeOptions[];
chromeOptions.addArguments["load-extension=/path/to/extension"];
driver = new ChromeDriver[chromeOptions];
// Start Chrome with a packed extension
ChromeOptions chromeOptions = new ChromeOptions[];
chromeOptions.addExtensions[new File["local/path/to/extension.crx"]];
driver = new ChromeDriver[chromeOptions];
// Change the default files’ save location
FirefoxProfile firefoxProfile = new FirefoxProfile[];
String downloadFilepath = "c:temp";
firefoxProfile.setPreference["browser.download.folderList", 2];
firefoxProfile.setPreference["browser.download.dir", downloadFilepath];
firefoxProfile.setPreference["browser.download.manager.alertOnEXEOpen", false];
firefoxProfile.setPreference["browser.helperApps.neverAsk.saveToDisk", "application/msword, application/binary, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream"]];
FirefoxOptions firefoxOptions = new FirefoxOptions[];
firefoxOptions.setProfile[firefoxProfile];
driver = new FirefoxDriver[firefoxOptions];

Chủ Đề