Selenium
traverse from first sibling to, second sibling
("first sibling's locator/following-sibling::div")
when we have two items with same locator, how tell selenium to choose the second one?
(locator)[2]
to store the result in a list in stream:
.collect(Collectors.toList())
to take a screenshot
1. convert your driver object to screenshot object 2. use method: getScreenshotAs() File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
how to sum up the values in a table exp.
1. create an integer variable and initialize with 0. 2. make a for loop to iterate through all items you want to sum up
how to make a hub:
1. download selenium server jar 2. invoke it in command prompt and assign the hub role to it: C:\>java -jar file's name.java -role hub 3. cmd gives you an IP address with port number: "Nodes should register to http://10.1.1.21:4444/grid/register/" 4.copy this address and paste it to browser and add /console at its end: http://10.1.1.21:4444/grid/console
connect a node to HUB
1. using Remote desktop, connect(login) to remote device that you want to register as node 2. download selenium server.jar file in the node machine, as well. 3. open the command prompt of the node machine and write the command to register to the hub: 4. location:\>java -jar file's name.jar -role webdriver -hub http://10.1.1.21:4444/grid/register -port 5566 5. to execute test cases, Download chromedriver or geckodriver on node machine and place in the path where exactly selenium server file is located 6. add this part to cmd: Java -Dwebdriver.chrome.driver="C:\chromedriver.exe" -jar selenium ...
we have a list of strings, create an object of it's arraylist
ArrayList<String> names=new ArrayList<String>();
convert an array to arraylist
Arrays.asList(array) - example: List<String>names=Arrays.asList("hk","hm","jyfu","ghg");
how to compare two lists?
Assert.assertTrue(firstList.equal(secondList))
class used to chrome browser
ChromeOptions
class to customize the Chrome browser
DesiredCapabilities
tell chrome browser certificates (2)
DesiredCapabilities ch=DesiredCapabilities.chrome(); ch.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true); ch.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); ChromeOptions c=new ChromeOptions(); c.merge(ch); System.setProperty("webdriver.chrme.driver", "C:\\chromedriver.exe"); WebDriver driver=new ChromeDriver(c);
take screenshot
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(src,new File("C:\\Users\\Mary\\screenshot.png"));
method to store the source object to our local machine
FileUtils().copyFile(source,target)
convert string to integer
Integer.parsInt(string variable name)
Assert.assertTrue(condition, message) :-
It takes one boolean argument and String message. It Asserts that a condition is true. If it isn't, an AssertionError, with the given message, is thrown.
Assert.assertFalse(condition) :-
It takes one boolean arguments and checks that a condition is false, If it isn't, an AssertionError is thrown.
how to see dynamic elements in page(usage of javascript )
JavascriptExecutor js = (JavascriptExecutor)driver; String script = "return document.getElementById(\"autocomplete\").value;"; String text = (String) js.executeScript(script); System.out.println(text);
how to enter javascript to your code?
JavascriptExecutor js=(JavascriptExecutor)driver; String mum="return document.getElementById(\"fromCity\").value;"; String text=(String) js.executeScript(mum); System.out.println(text);
make the equal expression fo this" ArrayList<String> names2=new ArrayList<String>(); names2.add("Abhijeet"); names2.add("Don"); names2.add("Alekhya"); names2.add("Adam"); names2.add("Rama");
List<String> names2=Arrays.asList("Abhijeet","Don","Alekheya","Adam","Rama");
when you want to click many links and open in separate tabs
List<WebElement> links=firstColumn.findElements(By.tagName("a")); String clicker=Keys.chord(Keys.CONTROL,Keys.ENTER); for (int a=1;a<links.size();a++){ links.get(a).sendKeys(clicker); }
open each of several links in a new tab
List<webElements> tabs=driver.findElements(By.tagName("a")); String clickByTab=Key.chord(Keys.CONTROL,Keys.ENTER); for (int i=1,i<tabs.size(),i++) { tabs.get(i).sendKeys(clickByTab);
to give the HUB role to a machine:
Location:\>java -jar file's name -role hub
what is a dropdown class in selenium?
Select
select "H" from dropdown list (static dropdown)
Select s=new Select(driver.findElement(By.id("dropdown's id"))); s.selectByValue("H"); if in html the item has value="H"
switch selenium to child tabs
Set<String> abc=driver.getWindowHandles(); Iterator<String> it=abc.iterator(); String parentWindow=it.next(); String childWindow=it.next(); driver.switchTo().window(parentWindow); driver.switchTo().window(childWindow);
sending selenium to different tabs
Set<String> abc=driver.getWindowHandles(); Iterator<String> itt=abc.iterator(); while (itt.hasNext()) { driver.switchTo().window(itt.next()); System.out.println(driver.getTitle());
directly create a stream
Stream.of("hcb","whch","hjegdh","hbc")
when we have two " in a line
String jscode="document.getElementById(\"fromCity\"); add \ before each "
how to grab an element's value?
String opt=driver.findElement(By.xpath("//*[@id='checkBoxOption2\']")).getAttribute("value");
how to grab the alert box's text?
String text= driver.switchTo().alert().getText();
how to switch back to the parent window after closing the child window
SwitchTo(). Window(driver. WindowHandles[0])
see an element's value
System.out.println(driver.findElement(By.id("autocomplete")).getAttribute("value"));
what .jar is needed to support Assert
TestNG
choose a desired text from a dropdown
WebElement dropdown=driver.findElement(By.id("dropdown-class-example")); Select s=new Select(dropdown); s.selectByVisibleText("text");
limit the scope of web driver
WebElement footerPart=driver.findElement(By.id("gf-BIG")); List<WebElement> footerLinks=footerPart.findElements(By.tagName("a"));
drag and drop
WebElement source=driver.findElement(By.id("draggable")); WebElement target=driver.findElement(By.id("droppable")); Actions a=new Actions(driver); a.dragAndDrop(source, target).build().perform();
make an arrylist stream compatible(turn arraylist to stream)
arraylist name.stream()
javascript DOM
can extract hidden elements
how to add extra capabilities to chrome?
desiredcapabilities ch=DesiredCapabalities.chrome(); ChromOptions c=new chromeOptions; c.merge(ch);
how to tell chrome to accept insecure certifications (1)
desiredcapabilities ch=DesiredCapabalities.chrome(); ch.acceptinsecurecertificates(); ChromOptions c=new chromeOptions; c.merge(ch); System.setProperty("webdriver.chrme.driver", "C:\\chromedriver.exe"); WebDriver driver=new ChromeDriver(c);
formula to access special child of a html element , inside the locator
div:nth-child(3)
how to tell selenium to go to 7th child which has a <div> tag or click on 7th child item which has "div" tag name
div:nth-child(7)
when you need to import a package that doesn't exist in your environment:
download that package for example: org.apache.commos.io unzip it and place in your project
how to delete all cookies in your web browser
driver.manage().DeleteAllCookies();
delete a cookie that you know it's name
driver.manage().DeleteCookieNamed("cookie's name")
maximizing the browser
driver.manage().window().maximize();
click browset's Back button
driver.navigate().back();
how to shift selenium focus to alert ?
driver.switchTo().alert()
bring selenium to main window when you'd taken it to another window or frame
driver.switchTo().defaultContent();
switch selenium to a particular frame of page
driver.switchTo().frame("frame's name");
how to press a key by selenium
element.sendkeys(Keys.ENTER);
open links in separate tabs
for(int i=1;i<firstColumnLinks.size();i++) { String clickOnLinkTab=Keys.chord(Keys.CONTROL,Keys.ENTER); firstColumnLinks.get(i).sendKeys(clickOnLinkTab);
where you can register a node machine to the hub?
from the node machine
how to place a package in java project
go to eclipse and right click on your project/ properties/ java build path/ click on Add external jars button/ choose the files you unzipped
where to check the process of links(URL)s
inspect / network tab / XHR and see the status code
who is test architect
is the one who designs solutions
assertion will stop your script if
it reaches: fail
.map()
let us modify stream filter results
names is an arraylist write a code to display only the items that are more than 4 characters
names.Stream().filter(s->s.length>4).forEach(s->System.out.println(s));
names is an arraylist write a code to display only the first item that is more than 4 characters
names.Stream().filter(s->s.length>4).limit(1).forEach(s->System.out.println(s));
how to identify the second item with same locator, instead of writing index?
parent child addressing: parentLocator childLocator
move one step forward in eclipse debugging mode
press F6
choose opt from a dropdown
s.selectByVisibleText(opt);
what is stream
stream is a collection of strings
names2.stream().filter(s->s.startsWith("A")).limit(1).forEach(s->System.out.println(s)); what does "limit(1)" do here?
the sentence will only return one answer (the first one)
to retrieve a value
use "return": String script="return document.getElementById(\"fromCity\").value;";
when to use filter() method in streams?
when we want to apply conditions
go down through a list option until you reach: BENGAlURU INTERNATIONAL AIRPORT
while(!text.equalsIgnoreCase("BENGALURU INTERNATIONAL AIRPORT")) { driver.findElement(By.id("fromPlaceName")).sendKeys(Keys.DOWN); }