How can I click on a pop-up Calendar wizard in Selenium 2? - testing

I'm using selenium 2 for a project which allows for date selection using jsf calendar wizard from myfaces (jsf 1.1). Can some one help me the steps for date selection. Thank you in advance.
Date of Birht field screenshot
Calendar popup screenshot
Corresponding html being generated
<div class="content">
<input id="firstApplicant2:ApplicantContent-DateOfBirth" name="firstApplicant2:ApplicantContent-DateOfBirth" type="text" maxlength="10" class="dateStyle" onfocus="selectText('null', 'firstApplicant2:ApplicantContent-DateOfBirth')" onclick="selectText('null', 'firstApplicant2:ApplicantContent-DateOfBirth')"><span id="firstApplicant2:ApplicantContent-DateOfBirthSpan"></span>
<div id="firstApplicant2:ApplicantContent-DateOfBirthSpan_calendarDiv" class="jscalendar-DB-div-style" style="visibility: hidden;">
<span id="firstApplicant2:ApplicantContent-DateOfBirthSpan"></span>
<table style="width:250px;" class="jscalendar-DB-table-style">
<tbody>
<tr class="jscalendar-DB-title-background-style">
<td>
<table style="width:248px;">
<tbody>
<tr>
<td class="jscalendar-DB-title-style">
<span><span class="jscalendar-DB-title-control-normal-style"> <img src="/webapp/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/13080856/calendar.HtmlCalendarRenderer/DB/left1.gif" width="10px" height="11px" style="border:0px;">  </span><span class="jscalendar-DB-title-control-normal-style"> <img src="/webapp/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/13080856/calendar.HtmlCalendarRenderer/DB/right1.gif" width="10px" height="11px" style="border:0px;">  </span>  </span>
</td>
<td style="text-align:right;">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="jscalendar-DB-body-style"></td>
</tr>
<tr class="jscalendar-DB-today-style">
<td class="jscalendar-DB-today-lbl-style">
<span>Today is <a class="jscalendar-DB-today-style" title="Go To Current Month" href="#">06/15/2011</a></span>
</td>
</tr>
</tbody>
</table>
</div>
<div id="firstApplicant2:ApplicantContent-DateOfBirthSpan_selectMonthDiv" class="jscalendar-DB-div-style" style="visibility: hidden;"></div>
<div id="firstApplicant2:ApplicantContent-DateOfBirthSpan_selectYearDiv" class="jscalendar-DB-div-style" style="visibility: hidden;"></div><script type="text/javascript">
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar=new org_apache_myfaces_PopupCalendar();
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.initData.imgDir = "/webapp/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/13080856/calendar.HtmlCalendarRenderer/DB/";
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.initData.monthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.initData.dayName = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.initData.startAt = 0;
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.dateFormatSymbols.weekdays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.dateFormatSymbols.shortWeekdays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.dateFormatSymbols.shortMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.dateFormatSymbols.months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.dateFormatSymbols.eras = new Array("BC","AD");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.dateFormatSymbols.ampms = new Array("AM","PM");
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.initData.todayDateFormat = "MM/dd/yyyy";
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.initData.popupLeft = false;
firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar.init(document.getElementById('firstApplicant2:ApplicantContent-DateOfBirthSpan'));
</script><img src="/webapp/Images/upgrade/icon_calendar.jpg" style="vertical-align:bottom;" onclick="firstApplicant2_3AApplicantContent_2DDateOfBirthCalendarVar._popUpCalendar(this,document.getElementById('firstApplicant2:ApplicantContent-DateOfBirth'),'MM/dd/yyyy')">
</div>

Would it be acceptable to just update the input text field with the properly formatted date? Unless you are specifically testing that MyFaces widget, I think this should suffice.

Related

Insert multiple records at once from a table

I have this table which is loaded with data from the database and what the user has to do is select the one they want and fill in the data from both the select and the input, and that data can be saved in separate records. It is the first time I work with this so I don't know a bit about the process.
Currently my view I have it this way
<tbody>
#foreach($vsoftware as $cli)
<tr>
<td style="text-align: center;vertical-align: middle">
<input type="checkbox" name="id_software[]" value="{{$cli->id_software}}">
</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->nom_soft}}</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->marca}}</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->nombre_ver}}</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->num_version}}</td>
<td style="text-align: center;vertical-align: middle">
<select class="mr-sm-2" id="inlineFormCustomSelect" name="id_tipo_venta[]" >
<option value="" selected>sale...</option>
<option value="1">Key</option>
<option value="2">Instal</option>
<option value="3">Key + Instal</option>
<option value="3">Viruz</option>
</select>
</td>
<td style="text-align: center;vertical-align: middle">
<input name="obs_software[]" class=" form-control" type="text">
</td>
</tr>
#endforeach
my controller I try to form it like this:
$soft_instal = new Software_instalacion;
$soft_instal->id_instalacion = $instalaciones->id;
$soft_instal->id_historial = $historial->id;
$soft_instal->id_software = $request->id_software;
$soft_instal->id_usuario = $request->id_usuario;
$soft_instal->id_tipo_venta = $request->id_tipo_venta;
$soft_instal->obs_software = $request->obs_software;
dd($soft_instal);
$soft_instal->save();
The answer that the dd gives me is:
response
And the idea is that you can store it like this in the database:
way to save
The simplest way is that you can loop through your checkbox array.
Here check out the #Tim Lewis answer:
submit multiple arrays from html and combine their attribute into seperate collections and store in Laravel App
Also read about Laravel Mass Assignment:
https://laravel.com/docs/8.x/eloquent#mass-assignment
you can use
foreach($request->input('id_software') as $index => $item) {
$soft_instal = new Software_instalacion;
$soft_instal->id_instalacion = $instalaciones->id;
$soft_instal->id_historial = $historial->id;
$soft_instal->id_software = $item;
$soft_instal->id_usuario = $request->input('id_usuario')[$index];
$soft_instal->id_tipo_venta = $request->input('id_tipo_venta')[$index];
$soft_instal->obs_software = $request->input('obs_software')[$index];
$soft_instal->save(); }

Selenium XPath multiple selection

I want to find all elements in the table that match the following conditions:
-div text contains '2019';
-div class='excellent';
here is the HTML code excerpt:
<tr>
<td>Name of Person1</td>
<td>
<div class="testDate">21/12/2019</div>
<div class="excellent"></div>
</td>
</tr>
<tr>
<td>Name of Person2</td>
<td>
<div class="testDate">01/12/2017</div>
</td>
</tr>
I tried this solution:
//tr/td[2][div/text()='21/12/2019'][div[#class='starred']]
but I need the year only and not entirely date.
Use the below xpath.
//tr/td[div[contains(.,'2019')]and div[#class='excellent']]
Screenshot:

Posting to Servlet From Two Forms Within HTML Page

Solution: Just added null default values to the HTML parameters. Seems to have solved it!
So I have an admin page (HTML) with two forms. First form is to add to an SQL database and has 4 input boxes, second form deletes from a SQL database and has 1 input box. If I use the first form, everything is OK. But if I leave the first form empty and just put a value for the 2nd form, I get an NumberFormatException for String = "" I can only assume because the first form is empty. I'm trying to have the user either fill the first form and leave the 2nd empty, or vice versa. Any advice on how to better approach this would be great.
This is my HTML page
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Admin</title>
<link rel="stylesheet" href="myStyle.css">
</head>
<body>
<h1>Welcome to the admin page.</h1>
<h2>Please enter the new product to be added:</h2>
<form action="eShop" method="Post">
<table class="tableBox">
<tr>
<td align="left"><b>Item Code:</b></td>
<td align="left"><input type="number" name="code"></td>
</tr>
<tr>
<td align="left"><b>Item Name:</b></td>
<td align="left"><input type="text" name="name"></td>
</tr>
<tr>
<td align="left"><b>Item Price:</b></td>
<td align="left"><input type="number" name="price"></td>
</tr>
<tr>
<td align="left"><b>Is Taxable:</b></td>
<td align="left"><input type="text" name="taxable"></td>
<tr>
<td align="center" colspan="2"><input type="submit" name="action" value="Update!"></td>
</tr>
</table>
</form>
<h2>Enter the code of the product you want to delete:</h2>
<form action="eShop" method="Post">
<table class="tableBox">
<tr>
<td align="left"><b>Item Code:</b></td>
<td align="left"><input type="number" name="code"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="action" value="Delete!"></td>
</tr>
</table>
</form>
</body>
</html>
This is the section in my servlet that gets the parameters
else if(request.getParameter("action").equals("Delete!")) {
DataAccessImpl temp = new DataAccessImpl();
int code = Integer.parseInt(request.getParameter("code"));
temp.deleteItem(code);
}
This is the DataAccessImpl class used in the servlet, particularly the method that will delete the row.
public void deleteItem(int code) {
connect();
String deleteStm = ("DELETE FROM ProductCatalogue WHERE code = " + code);
PreparedStatement pstm = null;
System.out.println("Beginning to delete product from database...");
try {
pstm = conn.prepareStatement(deleteStm);
System.out.println("Delete complete!");
}
catch(SQLException e) {
e.printStackTrace();
}
}
in first form name your submit button like this:
<input type="submit" name="updateAction" value="Update!">
in second form name your submit button like this:
<input type="submit" name="deleteAction" value="Delete!">
Servlet Code should be like this:
else if(request.getParameter("deleteAction").equals("Delete!")) {
DataAccessImpl temp = new DataAccessImpl();
int code = Integer.parseInt(request.getParameter("code"));
temp.deleteItem(code);
}

can we use selenium when such a table is not having proper html like shown below?

Here is the table that I am using to get the table row element that has specific element such as the href that has 'Harvest' in text and also checking if text 'running' exists in the same table row.
<table id="execTable" class="tableHistory jobtable translucent">
<colgroup>
<col class="execid">
<col class="titlecol">
</colgroup>
<tbody>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</tbody>
<tr id="8571">
<td>8571</td>
<td class="titlecol">
<div id="hitdiv-8571" class="arrow"></div>
Harvest
</td>
<td>09-03-2015 09:45:04</td>
<td>-</td>
<td>2m 6s</td>
<td>running</td>
<td>view/restart</td>
</tr>
<tr id="8571-child" class="childRow" style="display: none;"></tr>
<tr id="8566">
<td>8566</td>
<td class="titlecol">
<div id="hitdiv-8566" class="arrow"></div>
mk
</td>
<td>09-03-2015 03:30:00</td>
<td>09-03-2015 04:16:50</td>
<td>46m 50s</td>
<td>succeeded</td>
<td>view/restart</td>
</tr>
<tr id="8555-child" class="childRow" style="display: none;"></tr>
</table>
I am not able to get the TRs.
WebElement table = driver.findElement(By.id("execTable"));
List<WebElement> trows = table.findElements(By.tagName("tr"));
List<WebElement> all = driver.findElements(By.xpath(".//*[#id='execTable']/*"));
for (WebElement a : all) {
if(a.getTagName().equalsIgnoreCase("tr")) { ....}
}
I was able to get the above code working. Thank you!

How can I reach the link inside TR using XPATH

This HTML code has menu of targets and you need to select one target. One of these targets is ANY TARGET to continue process.
<tr id="mainForm:nav-panel-tab-set:0:trigger-selectTable:0" class=" iceRowSelMouseOver" tabindex="0" onmouseover="this.className=' iceRowSelMouseOver';" onmouseout="Ice.enableTxtSelection(document.body); this.className='iceDatTblRow1 selectable-rowRow1 iceRowSel'" onmousedown="return Ice.preventTextSelection(event);" ondblclick="Ice.registerDblClick(this);" onclick="Ice.registerClick(this,'mainForm:nav-panel-tab-set:0:trigger-selectTableclick_row','mainForm:nav-panel-tab-set:0:trigger-selectTableclick_count','0','mainForm',200,true,event,false,'mainForm:nav-panel-tab-set:0:trigger-selectTablesel_rows','');">
<td class="iceDatTblCol1 selectable-rowCol1" scope="row">
<a id="mainForm:nav-panel-tab-set:0:trigger-selectTable_idx_0" class="iceHdnLnk" onfocus="return Ice.tblRowFocus(this, false);" onblur="return Ice.tblRowBlur(this);" href="#">
<img alt="" src="/ice/xmlhttp/css/xp/css-images/spacer.gif"/>
</a>
<span id="mainForm:nav-panel-tab-set:0:trigger-selectTable:0:j_idt1417" class="iceOutTxt">ANY TARGET</span>
</td>
<td class="iceDatTblCol2 selectable-rowCol2">
<span id="mainForm:nav-panel-tab-set:0:trigger-selectTable:0:j_idt1419" class="iceOutTxt select-icon">»</span>
</td>
</tr>
How can I reach the ANY TARGET to click on >> to add using XPATH?
Extra Information:
The previous code is for sub window to add target. the main window code is:
<tr class="icePnlGrdRow1 settings-tableRow1">
<td id="mainForm:nav-panel-tab-set:0:j_idt1305-2-0" class="icePnlGrdCol1 settings-tableCol1">
<table id="mainForm:nav-panel-tab-set:0:j_idt1326" class="icePnlGrd settingsLabel">
<tbody>
<tr class="icePnlGrdRow1 settingsLabelRow1">
<td id="mainForm:nav-panel-tab-set:0:j_idt1326-0-0" class="icePnlGrdCol1 settingsLabelCol1">
<span id="mainForm:nav-panel-tab-set:0:j_idt1327" class="iceOutTxt label">Target</span>
</td>
</tr>
<tr class="icePnlGrdRow2 settingsLabelRow2">
<td id="mainForm:nav-panel-tab-set:0:j_idt1326-1-0" class="icePnlGrdCol1 settingsLabelCol1">
<span id="mainForm:nav-panel-tab-set:0:j_idt1328" class="iceOutTxt sidenote">Target or group</span>
</td>
</tr>
</tbody>
</table>
</td>
<td id="mainForm:nav-panel-tab-set:0:j_idt1305-2-1" class="icePnlGrdCol2 settings-tableCol2">
<table id="mainForm:nav-panel-tab-set:0:j_idt1329" class="icePnlGrd search">
<tbody>
<tr class="icePnlGrdRow1 searchRow1">
<td id="mainForm:nav-panel-tab-set:0:j_idt1329-0-0" class="icePnlGrdCol1 searchCol1">
<span id="mainForm:nav-panel-tab-set:0:j_idt1330" class="iceOutTxt"/>
</td>
<td id="mainForm:nav-panel-tab-set:0:j_idt1329-0-1" class="icePnlGrdCol2 searchCol2">
<input id="mainForm:nav-panel-tab-set:0:j_idt1331" class="iceCmdBtn" type="image" src="/ice/img/search-icon.png" onfocus="setFocus(this.id);" onclick="iceSubmitPartial(form, this, event);return false;" onblur="setFocus('');" name="mainForm:nav-panel-tab-set:0:j_idt1331"/>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
I used ID to click on the targets to open the sub window
//*[#id='mainForm:nav-panel-tab-set:0:j_idt1331']
However, I did not get any response to add any target to my main window either I used the solutions you mentioned.
You can reach the span containing "ANY TARGET":
//span[text() = "ANY TARGET"]
You can extend this to reach the span containing ">>" in the td immediately following the td that contains "ANY TARGET":
//td[span[text() = "ANY TARGET"]]/following-sibling::td[1]/span[text() = ">>"]
Try to focus on the first TD and then click on the Span element.
IWebElement td = browser_drive.FindElement(By.Id("mainForm:nav-panel-tab-set:0:trigger-selectTable:0")).FindElements(By.TagName("td"))[0];
IWebElement anytext = td.FindElement(By.Id("mainForm:nav-panel-tab-set:0:trigger-selectTable:0:j_idt1417"));
OpenQA.Selenium.Interactions.Actions act = new OpenQA.Selenium.Interactions.Actions(browser_drive);
act.MoveToElement(td).Click(anytext);
(Coded using C#, there must be an alternative. Pseudo code, not complied)