Selenium Revision

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Selinium Revision :

1.open browser:
WebDriver driver = new FirefoxDriver();
2.open website:
driver.get("Https://www.google.com/");
3.Locating the WebElements: - Attributes
ID, Name, ClassName, TagName, LinkText, XPath, CSSSelector.
4.Locator Sintax:
WebElement ObjectName = driver.FindElement(By.ID("Idname"));
5. CSSSelector Syntax: - we can use CSSSelector locator to get the CSS value-
EX:Background color. (it is mainly
used for finding the CSS property)
syntax: String Colour = getCssValue("Background-Color");
6. XPath usage reason: - it is used because some of the attributes of the web
elements are same, so mostly
we use XPath as a locator. Inspect Element - Copy - XPath.
7.Different types of WebElements :
TextBox(single line/MultipleLine), Links,LinkText(inside AncorTag), Button,
RadioButton,
DropDown(Select), CheckBox, alert(alert).
8.to insert Text in the TextBox: - SendKeys is used.
driver.FindElement(By.Id("IDname")).SendKeys("string we need to enter in the
textbox")
9.To Click the Link:
driver.FindElement(By.LinkText("GoTo Home Page")).Click(); //PartialLinkText also
available
10.To Click the Button:
driver.FindElement(By.CSSSelector("input[Value='Valuename']")).click();
11.To find the colour of the button:
WebElement ButtonColor =
driver.FindElement(By.CSSSelector("input[Value='Valuename']"));
String Color = ButtonColor.getCssValue("Background-Color");
System.Out.Println("The Button Color is:" +Color);
12.To find the position of the button:
WebElement ButtonPosition = driver.FindElement(By.Id("Id-1"));
Point Position = ButtonPosition.getlocation();
int XPosition = position.getX();
int YPosition = position.grtY();
System.Out.Println("X value is:"+XPosition+"Y Value is:"+YPosition);
}}
13.To Find the size of the button:-Width and Height
WebElement ButtonSize = driver.FindElement (By.name("Button1"));
int height = ButtonSize.height();
int width = ButtonSize.Width();
System.Out.Println("Button Height:"+height+"Button Width:"+width);
14.CheckBox:-To select the single checkbox:
WebElement CheckBox = driver.FindElement(By.Id("Id-1")).click();
15.CheckBox:-To select the multiple checkbox:
List <WebElement> MultipleCheckbox = driver.FindElement(By.Id("Checkbox-id"));
int size = MultipleCheckbox.size();
System.Out.Println("number of checkbox is:"+size);
for(i=1;i<size(i);i++);
{
MultipleCheckBox.get(i).Click();
}
16.RadioButton:-To click one(first) RadioButton:
driver.FindElement(By.CSSSelector("input[value='first option']")).click;
17.RadioButton:-To click multiple radio button one by one:
List <WebElements> MultiRadioButton =
driver.FindElement(By.CSSSelector("input[value='Radio options']"));
int size = MultiRadioButton.size();
System.Out.Println("Number of RadioButton is:"+size);
for(i=1;i<size(i);i++);
{
MultiRadioButton.get(i).Click();
}
18.Navigate commands:- To navigate the web page:
driver.navigate()Refresh();
driver.navigate()Forward();
driver.navigate()Back();
19.Alerts:when clicking the button the alert box will get appear.
there are three types of alerts are there:
a)normalAlert - only OK button will be available
b)ConfirmationAlert - OK button and CANCEL button are availavle.
c)Prompt
d)Linebrakes
a.NORMAL ALERT:
Webelement button = driver.findelement(By.ID("id-1"));
button.Click();
alert alertbox = driver.SwitchTo().alert();
alertbox().accept();
b.CONFIRMATION ALERT:
Webelement button = driver.findElement(By.Id("id-1"));
button.click();
alert alertbox = driver.SwitchTo().alert();
alertbox().dismiss();
c.PROMPT:
webelements button = driver.findelement(By.Id("Id-1"));
button.click();
alert alertbox = driver.SwitchTo().alert();
alertbox.SendKeys("Sheik Abdullah");
alertbox.accept();
d.LINEBRAKES:
alert alertbox = driver.SwitchTo().Alert();
20.Windows:
It is nothing but when we click the button, one seperate window will open,
our duty is to automate
the process of changing the window from parent to new opened window.
Exersice1: switch the action from oldwindow to newwindow and operation on new
window.
string parentwindow = driver.getwindowhandle();
Webelement button = driver.findelement(By.Id("Id-1"));
button.click();
set<string> driverhandles = driver.getwindowhandles(); //It will store both new and
old windows
for(string newwindow : driverhandles) //both old and new windows are stored in
driverhanldes, here for
new window it will go to for loop
{
driver.switchto().window(newwindow);
}
Webelement editbox = driver.findelement(By.Id("id-1"));
editbox.click();
driver.close(); //it will close the current new window
driver.switchto().window(oldwindow); it will switch from old to new window
}}
Exersice2: Know the number of opened window:
string parentwindow = driver.getwindowhandle();
webelement button = driver.findelement(By.id("id-1"));
button.click();
string driverhandles = driver.getwindowhandles();
int numofwindows = windowhandles.size();
system.Out.Println("Number of windows is:" +numofwindows);
}}
Exersice3: Close all the window except the parent window:
string parentwindow = driver.getwindowhandle();
Webelements button = driver.findelement(By.Id("Id-1"));
button.click();
set<String> windowhandles = driver.getwindowhandles();
for(string allwindows : windowhandles) //this will itrates all window one by one
{
if (!allwindows.equals(parentwindow))
{
driver.SwitchTo().window(allwindows);
driver.close;
}
}
21.Frames:
Ex:Press the button in the frame
driver.switchTo().frame(0);
webelement.button = driver.findelement(By.Id(Id-1));
button.click();
}}
Ex:press the button inside the frame which is inside the another frame:
driver.switchTo().frame(0);
driver.switchTo().frame(1);
webelement button = driver.findelement(By.Id("Id-1"));
button.click();
}}
Ex:Find the total number of frames:
List<webelements> numberofframes = driver.findelements(By.tagname("iframe"));
int size = numberofframes.size();
System.Out.Println("number of frames:"+size);
}}
22.Droppable: Drag and drop one element in the web page:
webelement from = driver.findelement(by.id(Id-1));
webelement to = driver.findelement(by.id(Id-2));
actions action = new actions(driver);
action.clickandhold(from).MovetoElement(to).release(to).build().perform();
or
action.draganddrop(from, to).build().perform();
22.Tooltip: whenever we keep the mouse pointer on any button it will throw message:
read and print the message:
webelement button = driver.findelement(By.Id("Id-1"));
string tooltip = button.getAttribute("Title");
System.Out.Println("the tooltip message is:"+tooltip );
}}
23.Multiple Select:
List<webelement> list = driver.findelements(by.Xpath("//*[@id=selectable]//li"));
Actions action = new actions(driver);
action.KeyDown(Keys.CONTROL).click(list.get(0)).click(list.get(1)).click(list.get(2
)).build().perfprm();
}}
24.AutoCompletion: when we press starting letter in textbox-it will show us
suggestion:
webelements textbox = driver.findelement(By.Id("id-1"));
textbox.SendKeys("s");
List<webelements> textboxlinks =
driver.findelements(By.Xpath("//*[@id=selectable//li]"));
for(webelement we : textboxlinks)
{
if(we.getText().equals("Selenium"))
{
we.click();
break;
}
}
25.Download file and verify the download:
webelement button = driver.findelement(By.id("id-1"));
button.click();
file filelocation = new file(C:\\users\sheik\downloads);
file[] files = filelocation.Filelist();
for(file filename : files) \\iterating the files one by one
{
if(filename.equals("downoad.xls"))
{
system.out.println("file got downloaded");
break;
}
26.uploading of file from windows to webpage:
webelement uploadbutton = driver.findelement(By.Id("id-1"));
uploadbutton.click();
string file="C:\\users\sheik\downloads\download.xml"
stringselection selection = new stringselection("file");
Toolkit.DefaultToolkit().getsystemclipboard().setcontents(selection,null);
Robot robot = new robot();
robot.Keypress(KeyEvent.VK_CONTROL);
robot.Keypress(keyEvent.VK_V);
robot.Keyrelease(KeyElement.VK_V);
robot.KeyRelease(KeyElement.VK_CONTROL);
Robot.Keypress(KeyElement.VK_ENTER);
}
27.Working with images: test whether the image is broken or not:
webelement brokenimage = driver.findelement(By.XPath("path address"));
if(brokenimage.GetAttributes("naturalwidth").equals(0))
{
System.Out.Println("the image is broken");
}else{
System.Out.Println("the image is not broken");
}
}
28.working with table:
Exersice1:type the number of rows:-
list<webelement> rows = driver.findelements(By.tagname("tr"));
int numberofrows = rows.size();
System.out.println("number of rows is:"+numberofrows);
Exersice2:Find the number of columns:-
List<webelement> columns = driver.findelement(By.tagname("th"));
int numberofcolumns = columns.size();
System.Out.Println("number of columns is :"+numberofcolumns);
Exersice3:print the percentage of the "Learn to intract with the element"
webelement percent = driver.findelement
(By.XPath("//td[normalize-space()='Learn to intract with
element']/following::td[1]"));
string percentage = percentage.gettext();
}}
Exersice4:Check the vital task of lest completed:-
List<Webelement> Progresspercent = driver.findelement(By.XPath("//td[2]"));

List<integer> numberlist = new.ArrayList<Integer>();

for (webelement webelement : progresspercent){


String individualvalue = webelement.getText();
numberlist.add(integer.parseint(individualvalue))
}
system.Out.Println("list of percent value is:"+numberlist);
}
int smallvalue = Collections.min(numberlist);
string smallValueString = integer.tostring(smallvalue)+"%";
string finalXpath
="//[normalize-space="+"\""smallValueString+"\""+"]//following::td[1])"
webelement checkbox = driver.findelement(By.XPath(finalXPath));
checkbox.click();
}

29.working with Calender:


Exersice : select the 10th day of following month:
webelement nextbutton = driver.findelement(By.Xpath("a[@title='next']"));
nextbutton.click();
webelement.datetobegiven = driver.findelement(By.XPath("a[contains(text(),'10')]"))
datetobegiven.click();
30.Working with sortables:
list<webelement> elements =
driver.findelements(By.XPath("//*[@id='sortable']/li"));
webelement from = elements.get(3);
webelement to =elements.get(1);
actions action = new actions(driver);
action.clickandhole(from);
action.movetoelement(to);
action.release(to);
action.build().perform();
}

You might also like