Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CRIO_BYTES
SELENIUM
me_selenium_web_actions_web_tables
Commits
f9ef163c
Commit
f9ef163c
authored
3 years ago
by
Nabhan Abdulla P V
💬
Browse files
Options
Download
Email Patches
Plain Diff
Update StaticTableSubjectTopper.java
parent
8c84d0a3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
229 additions
and
229 deletions
+229
-229
SeleniumWebActionsWebTableSolution/lib/src/main/java/webActions/StaticTableSubjectTopper.java
...ib/src/main/java/webActions/StaticTableSubjectTopper.java
+229
-229
No files found.
SeleniumWebActionsWebTableSolution/lib/src/main/java/webActions/StaticTableSubjectTopper.java
View file @
f9ef163c
package
webActions
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.util.List
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.remote.BrowserType
;
import
org.openqa.selenium.remote.DesiredCapabilities
;
import
org.openqa.selenium.remote.RemoteWebDriver
;
public
class
StaticTableSubjectTopper
{
WebDriver
driver
=
null
;
/**
* use this method to initialize the browser.
*/
public
WebDriver
startBrowser
()
throws
MalformedURLException
{
// Code to Launch Browser using Zalenium in Crio workspace
final
DesiredCapabilities
capabilities
=
new
DesiredCapabilities
();
capabilities
.
setBrowserName
(
BrowserType
.
CHROME
);
driver
=
new
RemoteWebDriver
(
new
URL
(
"http://localhost:8082/wd/hub"
),
capabilities
);
return
driver
;
}
/**
* use this method to open the url of an application
*/
public
void
openURL
(
String
browserURL
)
{
System
.
out
.
println
(
"maximize the window.."
);
driver
.
manage
().
window
().
maximize
();
System
.
out
.
println
(
"opening website --->"
+
browserURL
);
driver
.
get
(
browserURL
);
}
/**
* use this method to click on tabs
*
* @param webElement
*/
public
void
clickSubjectTopperTab
(
WebElement
webElement
)
{
webElement
.
click
();
}
/**
* use this method to print the columns name
*/
public
void
printColumnsName
(
List
<
WebElement
>
webElement
)
{
// TODO : Print the column name
System
.
out
.
println
(
"Column headers are : "
);
for
(
WebElement
element
:
webElement
)
{
System
.
out
.
print
(
"|"
+
element
.
getText
());
}
// To add next line
System
.
out
.
println
(
"\n"
);
}
/**
* use this method to print the total rows
*/
public
void
printTotalRows
(
List
<
WebElement
>
webElement
)
{
int
rowCount
=
0
;
// TODO : Print the rowCount
rowCount
=
webElement
.
size
();
System
.
out
.
println
(
"Row count is: "
+
rowCount
);
}
/**
* use this method to print the second row
*/
public
void
printSecondRow
(
List
<
WebElement
>
webElement
)
{
// TODO : Print the data
System
.
out
.
println
(
"Second row is : "
);
for
(
WebElement
element
:
webElement
)
{
System
.
out
.
print
(
"|"
+
element
.
getText
());
}
// To add next line
System
.
out
.
println
(
"\n"
);
}
/**
* Use this method to print the subject info
*
* @param webElement : List of table header columns
* @param headerName : Name of the table column
* @param value : to be search under column header
*/
public
void
printStudentInfo
(
List
<
WebElement
>
webElement
,
String
headerName
,
String
value
)
{
int
columnCounter
=
0
;
int
rowCounter
=
0
;
// TODO : Locate all the table headers
List
<
WebElement
>
theads
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']/thead//child::th"
));
// TODO : Iterate through each header to find out the matching header
for
(
WebElement
thead
:
theads
)
{
columnCounter
++;
if
(
thead
.
getText
().
equals
(
headerName
))
{
break
;
}
}
// TODO : Use the column header index to locate all the row data of the column
List
<
WebElement
>
subjects
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']/thead//child::th["
+
columnCounter
+
"]//ancestor::thead//following-sibling::tbody//child::td["
+
columnCounter
+
"]"
));
// TODO : Iterate through each row data of the column to find out the matching
// row data and print its info
for
(
WebElement
sub
:
subjects
)
{
rowCounter
++;
if
(
sub
.
getText
().
equals
(
value
))
{
List
<
WebElement
>
StudentInfo
=
driver
.
findElements
(
By
.
xpath
(
"//table//tbody//tr["
+
rowCounter
+
"]//td"
));
System
.
out
.
println
(
"Student record for suject : "
+
value
);
for
(
WebElement
info
:
StudentInfo
)
{
System
.
out
.
print
(
"|"
+
info
.
getText
());
}
}
}
}
/**
* use this method to calculate average of "Highest Marks"
*/
public
void
avgHighestMarks
(
List
<
WebElement
>
webElement
)
{
int
avg
=
0
;
int
totalRow
=
0
;
int
totalMarks
=
0
;
// TODO: Calculate the avg
totalRow
=
webElement
.
size
();
for
(
WebElement
element
:
webElement
)
{
String
value
=
element
.
getText
();
totalMarks
=
totalMarks
+
Integer
.
parseInt
(
value
);
}
avg
=
totalMarks
/
totalRow
;
// TODO: Print the avg
System
.
out
.
println
(
"Average marks are : "
+
avg
);
}
/**
* use this method to close the current window of browser
*/
public
void
closeBrowser
()
{
System
.
out
.
println
(
"Closing the browser window"
);
driver
.
close
();
}
public
static
void
main
(
String
[]
args
)
throws
MalformedURLException
{
// Create the object of StaticTableSubjectTopper class
StaticTableSubjectTopper
staticTable
=
new
StaticTableSubjectTopper
();
// Step - 1 : Call the method startBrowser
WebDriver
driver
=
staticTable
.
startBrowser
();
// Step - 2 : Call the method openURL
staticTable
.
openURL
(
"https://web-locators-static-site-qa.vercel.app/Web%20Table"
);
// TODO: Step - 3 Click on "Static Table - Subject Topper"
// WebElement tab;
WebElement
tab
=
driver
.
findElement
(
By
.
xpath
(
"//span[text()='Static Rable - Subject Topper']"
));
staticTable
.
clickSubjectTopperTab
(
tab
);
// Step - 4 : List/Print the names of all columns
// List<WebElement> columnsName;
// TODO: Locate the <th>under <thead> and assign it to columnName
List
<
WebElement
>
columnsName
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']//child::thead//th"
));
// TODO: Call the method printColumnsName() to print the column names
staticTable
.
printColumnsName
(
columnsName
);
// Step - 5 : List/Print the data in the second row
// List<WebElement> secondRowData;
// Locate the all <td> under second <tr> of <tbody> and assign it to
// secondRowData
List
<
WebElement
>
secondRowData
=
driver
.
findElements
(
By
.
xpath
(
"//table//tbody//child::tr[2]//td"
));
// Call the method printSecondRow()
staticTable
.
printSecondRow
(
secondRowData
);
// Step - 6 : Print the Student Name,Roll Number and the Mark of topper in
// "Java"
// List<WebElement> headers;
// TODO: Find all header columns
List
<
WebElement
>
headers
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']//thead//child::th"
));
// TODO: Call the method printStudentInfo() to print the topper student data
staticTable
.
printStudentInfo
(
headers
,
"Subject"
,
"JAVA"
);
// Step - 7 : Find the number of rows
// List<WebElement> totalRows;
// TODO: Locate all the <tr> under <tbody> and assign it to totalRows
List
<
WebElement
>
totalRows
=
driver
.
findElements
(
By
.
xpath
(
"//table//tbody//tr"
));
// TODO: Call the method printTotalRows() to the print the row count
staticTable
.
printTotalRows
(
totalRows
);
// Step - 8 : Find the average of all the "Highest Marks"
// List<WebElement> avgHighestMarks;
// TODO: Locate the column Highest Marks and assign it to avgHighestMarks.
List
<
WebElement
>
avgHighestMarks
=
driver
.
findElements
(
By
.
xpath
(
"//table//thead//child::th[text()='Highest Marks']//ancestor::thead//following-sibling::tbody//child::td[3]"
));
// TODO: Call the method avgHighestMarks()
staticTable
.
avgHighestMarks
(
avgHighestMarks
);
// Call the method closeBrowser
staticTable
.
closeBrowser
();
}
}
\ No newline at end of file
package
webActions
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.util.List
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.remote.BrowserType
;
import
org.openqa.selenium.remote.DesiredCapabilities
;
import
org.openqa.selenium.remote.RemoteWebDriver
;
public
class
StaticTableSubjectTopper
{
WebDriver
driver
=
null
;
/**
* use this method to initialize the browser.
*/
public
WebDriver
startBrowser
()
throws
MalformedURLException
{
// Code to Launch Browser using Zalenium in Crio workspace
final
DesiredCapabilities
capabilities
=
new
DesiredCapabilities
();
capabilities
.
setBrowserName
(
BrowserType
.
CHROME
);
driver
=
new
RemoteWebDriver
(
new
URL
(
"http://localhost:8082/wd/hub"
),
capabilities
);
return
driver
;
}
/**
* use this method to open the url of an application
*/
public
void
openURL
(
String
browserURL
)
{
System
.
out
.
println
(
"maximize the window.."
);
driver
.
manage
().
window
().
maximize
();
System
.
out
.
println
(
"opening website --->"
+
browserURL
);
driver
.
get
(
browserURL
);
}
/**
* use this method to click on tabs
*
* @param webElement
*/
public
void
clickSubjectTopperTab
(
WebElement
webElement
)
{
webElement
.
click
();
}
/**
* use this method to print the columns name
*/
public
void
printColumnsName
(
List
<
WebElement
>
webElement
)
{
// TODO : Print the column name
System
.
out
.
println
(
"Column headers are : "
);
for
(
WebElement
element
:
webElement
)
{
System
.
out
.
print
(
"|"
+
element
.
getText
());
}
// To add next line
System
.
out
.
println
(
"\n"
);
}
/**
* use this method to print the total rows
*/
public
void
printTotalRows
(
List
<
WebElement
>
webElement
)
{
int
rowCount
=
0
;
// TODO : Print the rowCount
rowCount
=
webElement
.
size
();
System
.
out
.
println
(
"Row count is: "
+
rowCount
);
}
/**
* use this method to print the second row
*/
public
void
printSecondRow
(
List
<
WebElement
>
webElement
)
{
// TODO : Print the data
System
.
out
.
println
(
"Second row is : "
);
for
(
WebElement
element
:
webElement
)
{
System
.
out
.
print
(
"|"
+
element
.
getText
());
}
// To add next line
System
.
out
.
println
(
"\n"
);
}
/**
* Use this method to print the subject info
*
* @param webElement : List of table header columns
* @param headerName : Name of the table column
* @param value : to be search under column header
*/
public
void
printStudentInfo
(
List
<
WebElement
>
webElement
,
String
headerName
,
String
value
)
{
int
columnCounter
=
0
;
int
rowCounter
=
0
;
// TODO : Locate all the table headers
List
<
WebElement
>
theads
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']/thead//child::th"
));
// TODO : Iterate through each header to find out the matching header
for
(
WebElement
thead
:
theads
)
{
columnCounter
++;
if
(
thead
.
getText
().
equals
(
headerName
))
{
break
;
}
}
// TODO : Use the column header index to locate all the row data of the column
List
<
WebElement
>
subjects
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']/thead//child::th["
+
columnCounter
+
"]//ancestor::thead//following-sibling::tbody//child::td["
+
columnCounter
+
"]"
));
// TODO : Iterate through each row data of the column to find out the matching
// row data and print its info
for
(
WebElement
sub
:
subjects
)
{
rowCounter
++;
if
(
sub
.
getText
().
equals
(
value
))
{
List
<
WebElement
>
StudentInfo
=
driver
.
findElements
(
By
.
xpath
(
"//table//tbody//tr["
+
rowCounter
+
"]//td"
));
System
.
out
.
println
(
"Student record for suject : "
+
value
);
for
(
WebElement
info
:
StudentInfo
)
{
System
.
out
.
print
(
"|"
+
info
.
getText
());
}
}
}
}
/**
* use this method to calculate average of "Highest Marks"
*/
public
void
avgHighestMarks
(
List
<
WebElement
>
webElement
)
{
int
avg
=
0
;
int
totalRow
=
0
;
int
totalMarks
=
0
;
// TODO: Calculate the avg
totalRow
=
webElement
.
size
();
for
(
WebElement
element
:
webElement
)
{
String
value
=
element
.
getText
();
totalMarks
=
totalMarks
+
Integer
.
parseInt
(
value
);
}
avg
=
totalMarks
/
totalRow
;
// TODO: Print the avg
System
.
out
.
println
(
"Average marks are : "
+
avg
);
}
/**
* use this method to close the current window of browser
*/
public
void
closeBrowser
()
{
System
.
out
.
println
(
"Closing the browser window"
);
driver
.
close
();
}
public
static
void
main
(
String
[]
args
)
throws
MalformedURLException
{
// Create the object of StaticTableSubjectTopper class
StaticTableSubjectTopper
staticTable
=
new
StaticTableSubjectTopper
();
// Step - 1 : Call the method startBrowser
WebDriver
driver
=
staticTable
.
startBrowser
();
// Step - 2 : Call the method openURL
staticTable
.
openURL
(
"https://web-locators-static-site-qa.vercel.app/Web%20Table"
);
// TODO: Step - 3 Click on "Static Table - Subject Topper"
// WebElement tab;
WebElement
tab
=
driver
.
findElement
(
By
.
xpath
(
"//span[text()='Static Rable - Subject Topper']"
));
staticTable
.
clickSubjectTopperTab
(
tab
);
// Step - 4 : List/Print the names of all columns
// List<WebElement> columnsName;
// TODO: Locate the <th>under <thead> and assign it to columnName
List
<
WebElement
>
columnsName
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']//child::thead//th"
));
// TODO: Call the method printColumnsName() to print the column names
staticTable
.
printColumnsName
(
columnsName
);
// Step - 5 : List/Print the data in the second row
// List<WebElement> secondRowData;
// Locate the all <td> under second <tr> of <tbody> and assign it to
// secondRowData
List
<
WebElement
>
secondRowData
=
driver
.
findElements
(
By
.
xpath
(
"//table//tbody//child::tr[2]//td"
));
// Call the method printSecondRow()
staticTable
.
printSecondRow
(
secondRowData
);
// Step - 6 : Print the Student Name,Roll Number and the Mark of topper in
// "Java"
// List<WebElement> headers;
// TODO: Find all header columns
List
<
WebElement
>
headers
=
driver
.
findElements
(
By
.
xpath
(
"//table[@aria-label='simple table']//thead//child::th"
));
// TODO: Call the method printStudentInfo() to print the topper student data
staticTable
.
printStudentInfo
(
headers
,
"Subject"
,
"Java"
);
// Step - 7 : Find the number of rows
// List<WebElement> totalRows;
// TODO: Locate all the <tr> under <tbody> and assign it to totalRows
List
<
WebElement
>
totalRows
=
driver
.
findElements
(
By
.
xpath
(
"//table//tbody//tr"
));
// TODO: Call the method printTotalRows() to the print the row count
staticTable
.
printTotalRows
(
totalRows
);
// Step - 8 : Find the average of all the "Highest Marks"
// List<WebElement> avgHighestMarks;
// TODO: Locate the column Highest Marks and assign it to avgHighestMarks.
List
<
WebElement
>
avgHighestMarks
=
driver
.
findElements
(
By
.
xpath
(
"//table//thead//child::th[text()='Highest Marks']//ancestor::thead//following-sibling::tbody//child::td[3]"
));
// TODO: Call the method avgHighestMarks()
staticTable
.
avgHighestMarks
(
avgHighestMarks
);
// Call the method closeBrowser
staticTable
.
closeBrowser
();
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment