Selenium IMportant Notes
Selenium IMportant Notes
List<WebElement> testlink_List=driver.findElements(By.locator(“”));
For(WebElement testlink : testlink_List){
URL link=new URL(testlink);
HttpURLConnection url=(HttpURLConnection)link.openConnection();
int code= url.getResponseCode();
if(code >=400)
System.out.println(“Links are broken”);
else
System.out.println(“Links are proper”);
}
Note :
In General 2xx :proper, 4xx and 5xx are errors.
400 :The request is invalid
401 :Unauthorized error
404 :The requested resource was not found.
500 :Internal Server Error
@Test
public void Amazon() {
System.setProperty("webdriver.gecko.driver", "D:\\Study
materials\\Setups\\geckodriver.exe");
FirefoxDriver driver=new FirefoxDriver();
driver.get("https://www.amazon.in/");
PageObjctClass page=PageFactory.initElements(driver, PageObjctClass.class);
WebDriverWait wait=new WebDriverWait(driver, 30);
page.YourAmazonIn.click();
}
}
Headless browser is a term used to define browser simulation programs which do not
have a GUI. These programs behave just like a browser but don’t show any GUI.
Selenium supports headless testing using its class called HtmlUnitDriver.
Once you have the Html unit driver you can just use it as a regular WebDriver.
4) How to handle HTTP Proxy Authentication ?
driver.get("http://UserName:[email protected]");
Save as .au3 format. Then Right click on the file and click Compile script to
generate .exe file.
As an alternate, Directly pass the File path to the Upload button via SendKeys().
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataText;
}
c = r.getCell(colNumber);
if(c == null)
c = r.createCell(colNumber);
c.setCellValue(value);
8) Selenium GRID
14.Nodes can be opened for various settings like browser, platform, version.
15.Now Selenium Hub and Nodes are created, lets make some @Test and execute
them.
16.Make Project on any machine, my project structure as below:
testng.xml
package myPackage;
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.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
public class BaseClass {
//ThreadLocal will keep local copy of driver
public static ThreadLocal<RemoteWebDriver> dr = new
ThreadLocal<RemoteWebDriver>();
@BeforeTest
//Parameter will get browser from testng.xml on which browser test to run
@Parameters("myBrowser")
public void beforeClass(String myBrowser) throws MalformedURLException{
RemoteWebDriver driver = null;
if(myBrowser.equals("chrome")){
DesiredCapabilities capability = new DesiredCapabilities().chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
capability);
}
else if(myBrowser.equals("firefox")){
DesiredCapabilities capability = new DesiredCapabilities().firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
capability);
}
//setting webdriver
setWebDriver(driver);
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
public WebDriver getDriver() {
return dr.get();
}
public void setWebDriver(RemoteWebDriver driver) {
dr.set(driver);
}
@AfterClass
public void afterClass(){
getDriver().quit();
dr.set(null);
}
}
TestParallel.java
package myPackage;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestParallel extends BaseClass {
@Test
public void test_01() throws InterruptedException, MalformedURLException{
try{
getDriver().get("http://www.w3schools.com/");
getDriver().findElement(By.xpath("html/body/div[2]/div/a[4]")).click();
//Wait intentially added to show parallelism execution
Thread.sleep(10000);
getDriver().findElement(By.xpath("//*[@id='gsc-i-
id1']")).sendKeys("test");
Thread.sleep(5000);
}
catch(Exception e){
System.out.println(e);
}
}
}
10) DataProvider
public class DataProviderTest {
private static WebDriver driver;
@DataProvider(name = "Authentication")
public static Object[][] credentials() {
return new Object[][] { { "testuser_1", "Test@123" }, { "testuser_1", "Test@123" }};
}
// Here we are calling the Data Provider object with its Name
@Test(dataProvider = "Authentication")
public void test(String sUsername, String sPassword) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.store.demoqa.com");
driver.findElement(By.xpath(".//*[@id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys(sUsername);
driver.findElement(By.id("pwd")).sendKeys(sPassword);
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();
driver.quit();
}
}