package com.SeleniumGridByCloud;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class RunSeleniumGridOnCloud {
public WebDriver driver;
public static final String USERNAME= "USERNAME";
public static final String AUTOMATE_KEY= "ACCESS_KEY";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY
+ "@hub-cloud.browserstack.com/wd/hub";
@BeforeClass
public void setUp() {
try {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browser", "IE");
caps.setCapability("browser_version", "10.0");
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "XP");
caps.setCapability("browserstack.debug", "true");
driver = new RemoteWebDriver(new URL(URL), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testSeleniumGridOnFirefox() {
try {
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(35, TimeUnit.SECONDS);
driver.get("https://seleniumcodeexample.blogspot.in");
System.out.println("Title Name is : " + driver.getTitle());
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
}
Selenium Code Example
Selenium WebDriver With Core Java Code Examples For Practice
Tuesday, 31 October 2017
Run Selenium Grid On Internet Explorer Browser
package com.seleniumGrid;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class RunSeleniumGridOnIE {
WebDriver driver;
String nodeUrl = "http://192.168.3.12:5555/wd/hub";
@BeforeClass
public void setUp() {
try {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_
IGNORING_SECURITY_DOMAINS , true); driver = new RemoteWebDriver(new URL(nodeUrl), capabilities); } catch (MalformedURLException e) { e.printStackTrace(); } } @Test public void testSeleniumGridOnIE() { try { driver.manage().deleteAllCookies(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES); driver.get("https://seleniumcodeexample.blogspot.in"); System.out.println("Title Name is : " + driver.getTitle()); } catch (Exception e) { e.printStackTrace(); } } @AfterClass public void tearDown() throws InterruptedException { Thread.sleep(2000); driver.quit(); } }
IGNORING_SECURITY_DOMAINS , true); driver = new RemoteWebDriver(new URL(nodeUrl), capabilities); } catch (MalformedURLException e) { e.printStackTrace(); } } @Test public void testSeleniumGridOnIE() { try { driver.manage().deleteAllCookies(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES); driver.get("https://seleniumcodeexample.blogspot.in"); System.out.println("Title Name is : " + driver.getTitle()); } catch (Exception e) { e.printStackTrace(); } } @AfterClass public void tearDown() throws InterruptedException { Thread.sleep(2000); driver.quit(); } }
Run Selenium Grid On Firefox Browser
package com.seleniumGrid;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class RunSeleniumGridOnFirefox {
WebDriver driver;
String nodeUrl = "http://192.168.3.12:1712/wd/hub";
@BeforeClass
public void setUp() {
try {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testSeleniumGridOnFirefox() {
try {
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(35, TimeUnit.SECONDS);
driver.get("https://seleniumcodeexample.blogspot.in");
System.out.println("Title Name is : " + driver.getTitle());
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
}
Run Selenium Grid On Chrome Browser
package com.seleniumGrid;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class RunSeleniumGridOnChrome {
WebDriver driver;
String nodeUrl = "http://192.168.3.12:1719/wd/hub";
@BeforeClass
public void setUp() {
try {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testSeleniumGridOnChrome() {
try {
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(35, TimeUnit.SECONDS);
driver.get("https://seleniumcodeexample.blogspot.in");
System.out.println("Title Name is : " + driver.getTitle());
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
}
Steps to Run Selenium Grid Hub and Node (Window7 OS)
There are some steps to start the Hub(Server) and then register the Node(OS) with Hub.
java -jar selenium-server-standalone-3.6.0.jar -role hub
========================================================================
@@@@ Run this Command for Node Configure with Chrome Browser in Window OS @@@@
java -Dwebdriver.chrome.driver=D:\SeleniumGrid\drivers\chromedriver.exe -jar selenium-server-standalone-3.6.0.jar -role node -hub http://192.168.3.12:4444/grid/register -browser browserName=chrome -port 5555
========================================================================
@@@@ Run this Command for Node Configure with Firefox Browser in Window OS @@@@
java -Dwebdriver.geckodriver.driver=D:\SeleniumGrid\drivers\geckodriver.exe -jar selenium-server-standalone-3.6.0.jar -role node -hub http://192.168.3.12:4444/grid/register -browser browserName=firefox -port 5555
========================================================================
@@@@@ Run this Command for Node Configure with IE Browser in Window OS @@@@@
java -Dwebdriver.ie.driver=D:\SeleniumGrid\drivers\IEDriverServer.exe -jar selenium-server-standalone-3.6.0.jar -role node -hub http://192.168.3.12:4444/grid/register -browser browserName=ie -port 5555
========================================================================
Run this Command for Node Configure with both firfox,ie and Chrome Browser in Window OS
java -Dwebdriver.chrome.driver=D:\SeleniumGrid\drivers\chromedriver.exe -Dwebdriver.gecko.driver=D:\SeleniumGrid\drivers\geckodriver.exe -Dwebdriver.ie.driver=D:\SeleniumGrid\drivers\IEDriverServer.exe
-jar selenium-server-standalone-3.6.0.jar -role node -hub http://192.168.3.12:4444/grid/register
Tuesday, 17 October 2017
Code Example - Disable Save Password Extension In Chrome Browser
package com.practiceCode;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
/**
* @Blog Name: Selenium Code Example
* @author Deepak Gupta
* @Created Date 17-10-2017
*
*/
public class DisableSavePasswordExtensionInChromeBrowser {
WebDriver driver;
@BeforeTest
public void setUp() {
try {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") +
"/drivers/chromedriver.exe"); // Handle - disable the save password Extension ChromeOptions options = new ChromeOptions(); Map<String, Object> prefs = new HashMap<String, Object>(); prefs.put("credentials_enable_service", false); prefs.put("profile.password_manager_enabled", false); options.setExperimentalOption("prefs", prefs); driver = new ChromeDriver(options); } catch (Exception e) { e.printStackTrace(); } } @Test public void testDisableSavePasswordExtensionInChromeBrowser() { driver.get("https://www.google.co.in"); } @AfterTest public void tearDown() throws InterruptedException { driver.quit(); } }
"/drivers/chromedriver.exe"); // Handle - disable the save password Extension ChromeOptions options = new ChromeOptions(); Map<String, Object> prefs = new HashMap<String, Object>(); prefs.put("credentials_enable_service", false); prefs.put("profile.password_manager_enabled", false); options.setExperimentalOption("prefs", prefs); driver = new ChromeDriver(options); } catch (Exception e) { e.printStackTrace(); } } @Test public void testDisableSavePasswordExtensionInChromeBrowser() { driver.get("https://www.google.co.in"); } @AfterTest public void tearDown() throws InterruptedException { driver.quit(); } }
Monday, 16 October 2017
Code Example - Disable SSL Certificate In Chrome Browser
package com.practiceCode;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
/**
* @Blog Name: Selenium Code Example
* @author Deepak Gupta
* @Created Date 17-10-2017
*
*/
public class DisableSSLCertificateInChromeBrowser {
WebDriver driver;
@BeforeTest
public void setUp() {
try {
// Handle https - Your connection is not private
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") +
"/drivers/chromedriver.exe"); driver = new ChromeDriver(); } catch (Exception e) { e.printStackTrace(); } } @Test public void testDisableSSLCertificateInChromeBrowser() { driver.get("https://www.google.co.in"); } @AfterTest public void tearDown() throws InterruptedException { driver.quit(); } }
"/drivers/chromedriver.exe"); driver = new ChromeDriver(); } catch (Exception e) { e.printStackTrace(); } } @Test public void testDisableSSLCertificateInChromeBrowser() { driver.get("https://www.google.co.in"); } @AfterTest public void tearDown() throws InterruptedException { driver.quit(); } }
Code Example - Disable Invalid Extension in Chrome Browser
package com.practiceCode;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
/**
* @Blog Name: Selenium Code Example
* @author Deepak Gupta
* @Created Date 17-10-2017
*
*/
public class DisableInvalidExtensionInChromeBrowser {
WebDriver driver;
@BeforeTest
public void setUp() {
try {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") +
"/drivers/chromedriver.exe"); // Handle - disable the invalid Extension ChromeOptions options = new ChromeOptions(); options.addArguments("disable-infobars"); driver = new ChromeDriver(options); } catch (Exception e) { e.printStackTrace(); } } @Test public void testDisableInvalidExtensionInChromeBrowser() { driver.get("https://www.google.co.in"); } @AfterTest public void tearDown() throws InterruptedException { driver.quit(); } }
"/drivers/chromedriver.exe"); // Handle - disable the invalid Extension ChromeOptions options = new ChromeOptions(); options.addArguments("disable-infobars"); driver = new ChromeDriver(options); } catch (Exception e) { e.printStackTrace(); } } @Test public void testDisableInvalidExtensionInChromeBrowser() { driver.get("https://www.google.co.in"); } @AfterTest public void tearDown() throws InterruptedException { driver.quit(); } }
Friday, 29 September 2017
Code Example - Image Save By Robot Class
package com.practiceCode;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 29-09-2017
*
*/
public class ImageSaveByRobotClass extends SuperTestNG {
@Test
public void testImageSaveByRobotClass() throws IOException, InterruptedException, AWTException {
System.out.println("========= Starting testImageSaveByRobotClass() ============");
try {
driver.get("http://only-testing-blog.blogspot.in/2014/09/selectable.html");
// Locate Image
WebElement Image = driver.findElement(By.xpath("//img[@border='0']"));
// Rihgt click on Image using contextClick() method.
Actions action = new Actions(driver);
action.contextClick(Image).build().perform();
StringSelection file = new StringSelection("D:\\TestingSaveImage.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(file, null);
Robot rb = new Robot();
for (int i = 1; i <= 7; i++) {
// Arrow down
rb.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(100);
}
// To press Enter key.
rb.keyPress(KeyEvent.VK_ENTER);
// Similar to thread.sleep
rb.setAutoDelay(1000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyRelease(KeyEvent.VK_V);
rb.setAutoDelay(1000);
// To press Enter key.
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
System.out.println("File Save Successfully.");
System.out.println("========= Ending testImageSaveByRobotClass() ============");
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
Code Example - Disable Firefox Console Log
package com.practiceCode;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
/**
*
* @Blog Name : Selenium Code Example
* @ Disable Firefox Console Log
* @author Deepak Gupta
* @Created Date 29-09-2017
*
*/
public class DisableFirefoxConsoleLog {
WebDriver driver;
@Test
public void testDisableFirefoxConsoleLog() {
try {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") +
"/drivers/geckodriver.exe"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null"); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get("https://seleniumcodeexample.blogspot.in/"); String title = driver.getTitle(); System.out.println("Title Name is : " + title); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } } @AfterTest public void tearDown() { driver.quit(); } }
"/drivers/geckodriver.exe"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null"); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get("https://seleniumcodeexample.blogspot.in/"); String title = driver.getTitle(); System.out.println("Title Name is : " + title); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } } @AfterTest public void tearDown() { driver.quit(); } }
Code Example - File Download By Robot Class In Firefox Browser
package com.practiceCode;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 29-09-2017
*
*/
public class FileDownloadByRobotClass extends SuperTestNG {
@Test
public void testFileDownloadByRobotClass() {
System.out.println("==========starting testFileDownloadByRobotClass() =========");
try {
driver.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
// Download Text File
driver.findElement(By.linkText("Download Text File")).click();
System.out.println("Download Text File link is clicked");
// downloadByRobotClass() method
downloadByRobotClass();
// press enter key of keyboard to perform above selected action
System.out.println("File Download successfully.");
System.out.println("==========ending testFileDownloadByRobotClass() =========");
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
public void downloadByRobotClass() throws AWTException, InterruptedException {
Robot robot = new Robot();
// For clicking on the Arrow Down on the dialog box
robot.keyPress(KeyEvent.VK_DOWN);
// For clicking on the Ok button on the dialog box
robot.keyPress(KeyEvent.VK_ENTER);
//robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(2000);
}
}
Subscribe to:
Posts (Atom)
Code Example - File Download By Robot Class In Firefox Browser
Exported from Notepad++ package com . practiceCode ; import java . awt . AWTException ; import java . awt . Robot ; import j...
-
package com.test.automation.uiAutomation.testBase; import java.io.File; import java.io.FileInputStream; import java.io.IOException...
-
Exported from Notepad++ package com . practiceCode ; import org . openqa . selenium . WebDriver ; import org . openqa . seleni...
-
package com.practiceCode; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import org.openqa.sel...