How to deal with byClassName in Selenium and PHPUnit - php

I have multiple elements with the same class name and no id, I need to get the first element with the class name. so lets say the html looks like this:
<table class="default-table">
<thead>
<tr>
<th class="cell"><span class="text"> Title1</span></th>
<th class="cell"><span class="text"> Title2</span></th>
<th class="cell"><span class="text"> Title3</span></th>
<th class="cell"><span class="text"> Title4</span></th>
</tr>
</thead>
<tbody class="tb">
<tr>
<td class="theInput">
<input type="text" data-type="string" value="" class="text " data-model-key="item1">
</td>
<td class="theInput">
<input type="text" data-type="string" value="" class="text " data-model-key="item2">
</td>
<td class="theInput">
<input type="text" data-type="string" value="" class="text " data-model-key="item3">
</td>
<td class="theInput">
<input type="text" data-type="string" value="" class="text " data-model-key="item4">
</td>
</tr>
</tbody>
</table>
Now, I want to set the value of the first and third input fields with the class "text", I found a solution if I was using java that looks something like this:
classElement = driver.findElements(By.className("text"));
classElement.get(1).value("1234");
classElement.get(3).value("5678");
I'm not sure that it would work as I am not familiar with java but it looks logical, any solution like this in PHP? Or is it a better way then calling the class to access the element in this case?

I found a solution that looks like this:
$items = $this->elements($this->using('css selector')->value('table.default-table input.text'));
$items[0]->value('1234');
$items[2]->value('5678');

Related

Not able to reset the input field jquery php wordpress

I need to reset the field onclick of reset button
But I am not able to do that.
Here is my html code
<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table1" id="form-table1" >
<tbody>
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_mailCc"><?php _e('OpenLab Cc', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_mailCc" name="approved_mailCc" type="email" value="<?php echo isset($item['approved_mailCc']) ? $item['approved_mailCc'] : ''; ?>"
size="50" class="code" placeholder="<?php _e('OpenLab Cc', 'custom_table_example')?>" required />
<input type="button" value="Reset" id="approved_mailCc" name="openLab"/>
</td>
</tr>
jQuery code
jQuery(document).ready(function($){
console.log("plugin script loaded2");
$('#approved_mailCc').click(function(){
$(this).val('');
});
});
1 I am getting console log.
2 alert is not going inside onclick
3 i used reset function also.
You have two inputs with the same ID of "approved_mailCc" you can't have more than one item with the same ID, it will confuse jQuery. Give them unique IDs like below:
<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table1" id="form-table1" >
<tbody>
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_mailCc"><?php _e('OpenLab Cc', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_mailCc_email" name="approved_mailCc" type="email" value="<?php echo isset($item['approved_mailCc']) ? $item['approved_mailCc'] : ''; ?>"
size="50" class="code" placeholder="<?php _e('OpenLab Cc', 'custom_table_example')?>" required />
<input type="button" value="Reset" id="approved_mailCc_button" name="openLab"/>
</td>
</tr>
and then use this jQuery code:
jQuery(document).ready(function(){
console.log("plugin script loaded2");
jQuery('#approved_mailCc_button').click(function(){
jQuery('#approved_mailCc_button_email').val('');
});
First, you are not supposed to need Javascript to reset a form (but you need the tag) :
https://jsfiddle.net/9xfonyou/
Second, there are errors on your code double IDs.
Here "approved_mailCc" is both your input file and your button. Then you can do this in Javascript to make it work.
HTML :
<form>
<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table1" id="form-table1">
<tbody>
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_mailCc"></label>
</th>
<td>
<input id="input_field" name="approved_mailCc" type="email" value="" size="50" class="code" placeholder="test" required />
<input type="reset" id="reset_button" name="openLab" />
</td>
</tr>
</tbody>
</table>
</form>
JS :
jQuery(document).ready(function($) {
console.log("plugin script loaded2");
$('#reset_button').click(function(e) {
e.preventDefault();
$('#input_field').val('');
});
});
https://jsfiddle.net/9xfonyou/1/

Count html table rows using PHP

I need to display the num of the row (tr) using php, but I don't know how to do this because I am using quotes.
$data['formation'] = '
<div class="table-responsive">
<table class="table table-bordered" style="width: auto !important;">
<thead style="font-weight: bold;">
<tr>
<td>N</td>
<td>Intitulé</td>
<td>Organisme</td>
<td>Date</td>
<td>Durée (en heures)</td>
<td>Eval. à chaud / à froid</td>
<td>Dispositif utilisé</td>
</tr>
</thead>
<tbody class="table-striped">
<tr>
<td>
**HERE NUM OF THE ROW**
</td>
<td>
<input type="text" class="form-control" name="form_intitule" id="form_intitule" readonly>
</td>
<td>
<input type="text" class="form-control" name="form_organisme" id="form_organisme" readonly>
</td>
<td>
<input type="text" class="form-control" name="form_date" id="form_date" readonly>
</td>
<td>
<input type="text" class="form-control" name="form_duree" id="form_duree" readonly>
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>';
}
This is my code:
foreach($html->find('table') as $table){
// returns all the <tr> tag inside $table $all_trs = $table->find('tr');
$count = count($all_trs); echo $count;
}
Try this:
$count = substr_count($data['formation'], "<tr>");
echo "Total rows: ".$count;
Though it's not a good solution but still simple to use. You can use substr_count
echo substr_count($data['formation'], '<tr>'); // 2
This is a bit of a workaround. But it works just as well.
print_r(array_count_values(str_word_count($data['formation'], 1)) );
What the above code does is count all words in the $data['formation'] string. Then, it will devide this into groups and put the results in an array.
It will then print the array and you can see how much times <tr> occurs.

clone column1 to column 2 using button

I need another name2 column where in it has a button(copy all) once you click it a dialog box will appear "Are you sure you want to copy |Yes/No". it'll clone the data from name to name2.
<table class="downpanel">
<thead>
<tr>
<th></th>
<th>Name</th>
<th></th>
<th>Name2</th>
<th></th>
<th colspan="">Count</th>
<th></th>
<th>Unit</th>
<th></th>
<th>Category</th>
<th></th>
<th>Data1</th>
<th></th>
<th>Data2</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="button" class="copyall">
</td>
<td>
<input type="text" size="25" name="name">
</td>
<td>
<input type="text" size="25" name="name2">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="3" name="from">
</td>
<td>
<input type="text" size="3" name="to">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<select name="unit" style="width: 75px;">
<option value="blah">blah</option>
</select>
<br />
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<select name="category" style="width: 275px;">
<option value="blah">blah</option>
</select>
<br />
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="10" id="datepicker" name="data1">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="7" name="data2">
</td>
<td>
<input type="button" class="copy">
</td>
</tr>
</tbody>
</table>
http://jsfiddle.net/jakecigar/hq2GG/2/
check this http://jsfiddle.net/hq2GG/4/
var r = confirm("Are you sure you want to copy?");
in both copy and copy all
http://jsfiddle.net/hq2GG/6/
You are facing a mode mash up issue. You initiated your data using HTML and now you want to manipulate it via Javascript. The more complex your issues get, the more you will end up in coding chaos. This is NOT about mixing different coding practices but to break the conceptual rework by designing one part in HTML and the other in Javascript. So as javascript seems to be mandatory here first design everything as if your whole application would only consist of Javascript. Define a mode:
DOM-Node-Manipulation (would be state of the art and also easier for manipulation) OR
html compile style (using innerHTML)
Once you got a mode make your matrix a matrix. If you need to copy cells, respectively rows and columns, you need to be able to address those. In the DOM Model you could maintain a two-dimensional wrapper array, in the html compile style you would obviously fittle with IDs, such as cell1_3, which helps you to make use of the getElementById. At the point in time you are able to address elements, it is just a matter of formulating loops, to get things copied, moved or deleted in bulk mode.
Once you got all of the conceptional prework you can decide to prefill your html page with html (and not javascript) - however this must follow the rules you set yourself for the scripting mode.

Display keywords from DB as Checkbox Options

I've created a database using mysqli and I have keywords stored in a table called "Keywords". I need to use the keywords stored in this table to display as options on a form in the form of checkboxes.
So, if the keyword has not been added to the db, then the checkbox on the form will not display for users to choose from. What's the best way to achieve this? My sql knowledge is basic but I can have an idea on how the query might look. Just not sure about how to display them as checkboxes.
UPDATE UPDATE UPDATE!
I was able to get my array to display with checkboxes: Here is what I did:
<?php
include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
print <<<HERE
HERE;
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
}
?>
NOW MY QUESTION IS: How do I pass the checked value off to my new table (profileTable) a completely separate table containing the form results?
MY FORM CODE LOOKS LIKE THIS (Fri 8/9/13):
<div id="form1profile">
<form method="post" enctype="multipart/form-data" name="addProfile" id="addProfile" >
<fieldset>
<legend>Add/Create a Media Source Profile</legend>
<br>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th ><label>First Name</label></th>
<td><input name="FName" type="text" id="FName" size="40"></td>
</tr>
<tr>
<th >Middle Name</th>
<td><input name="MName" type="text" id="MName" size="40"></td>
</tr>
<tr>
<th ><label>Last<br>
Name</label></th>
<td><input name="LName" type="text" id="LName" size="40"></td>
</tr>
<tr>
<th><label>Prefix</label></th>
<td><input name="Prefix" type="text" id="Prefix" size="40"></td>
</tr>
<tr>
<th><label>Title
</label></th>
<td><input name="Title" type="text" id="Title" size="40"></td>
</tr>
<tr>
<th><label>Dept</label></th>
<td><input name="Dept" type="text" id="Dept" size="40"></td>
</tr>
<tr>
<th><label>Phone</label></th>
<td><input name="PH1" type="text" id="PH1" size="40"></td>
</tr>
<tr>
<th><label>Other Phone</label></th>
<td><input name="PH2" type="text" id="PH2" size="40"></td>
</tr>
<tr>
<th><label>Email</label></th>
<td><input name="Email" type="text" id="Email" size="40"></td>
</tr>
<tr>
<th><label for="ProfileImg">Photo</label></th>
<td><input name="ProfileImg" type="file" multiple id="ProfileImg" form="addProfile">
</td>
</tr>
<tr>
<th valign="top"><label class="biotextTitle">Bio/Info</label></th>
<td><textarea name="bioLinks" cols="55" rows="25" id="bioLinks" placeholder="Paste Bio/Links here..."></textarea></td>
</tr>
<tr>
<th><label class="Keywords">Keyword</label></th>
<td>
<?php include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" name="keyword[$key]" id="$key">$key<br />
HERE;
}
?>
</td>
</tr>
<tr>
<th><label class="tags">Tags</label></th>
<td><input name="Tags" type="text" id="Tags" size="40"></td>
</tr>
<tr>
<th><input name="SaveBtn" type="submit" id="SaveBtn" formaction="insertProfile.php" formmethod="POST" value="Save"></th>
<td></td>
</tr>
</table>
<br />
</fieldset>
</form>
</div>
You may add a name to your checkboxes:
<input type="checkbox" name="keyword[$key]" value="1">$key<br />
Doing so, when the page is submit you just need to iterate through the selected keywords:
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
After that you can get all the selected keywords, then you shall just insert them into your table.
change this --
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
to this
echo "<input type='checkbox' id='tag_$id'>$key<br />";
reason for doing this is so you eliminate all character combinations by just using numeric only. character combo for EX. id="Ava/Flight", id="Ava Flight"

How to handle multiple actions using a single html form

I hope I can be clear enough in what I need here. What I have is a function that contains some html for an email client app I'm developing. This part of the app uses a common forward, reply and "reply to all" form. Since they are all basically the same, I figured I'd go the lean route and just use a single function to handle this. The only real differences I can see between the 3 actions I mentioned above are that in a reply to all, there will be multiple email addys in the CC part of the form. For a forward, no (cc) and the (to) box should be blank. I need to represent all of this functionality and I'm kind of confused on what the best way to do this is. Can anyone please offer any help? Thanks.
I can certainly post the html is need be, I just wanted to start light.
EDIT:
I almost forgot, there will be POST values when the user submits the form in the event that the form validation fails.
function compose($type,$contents=null)
{
echo'
<tr>
<td>
<tr>
<td valign="top">
<form method="post" action="">
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
<tr>
<td><h2>'.$type.'</h2></td>
</tr>
<tr>
<td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="" type="text" size="64"></td>
</tr>
<tr>
<td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
</tr>
<tr>
<td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
</tr>
<tr>
<td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
</tr>
</table>
</form>
</td>
</tr>
</td>
</tr>';
}
EDIT: Example modification of posted code (I haven't added all the different cases or even changed the output really, just showing the concept - all that's here is a check for a type of Reply and a check for the 'to' POST value.)
function compose($type,$contents=null)
{
$toValue = '';
if(isset($_POST['to']))
{
// Might want to actually validate this to prevent XSS, but this is just a basic example
$toValue = $_POST['to'];
}
echo'
<tr>
<td>
<tr>
<td valign="top">
<form method="post" action="">
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
<tr>
<td><h2>'.$type.'</h2></td>
</tr>';
if($type == "Reply") {
echo'
<tr>
<td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="' . $toValue . '" type="text" size="64"></td>
</tr>
<tr>
<td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
</tr>';
}
echo'
<tr>
<td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
</tr>
<tr>
<td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
</tr>
</table>
</form>
</td>
</tr>
</td>
</tr>';
}
(Original inquiry)
Is this function processing the results of the form, or is it displaying the form in the first place? Your question is a bit unclear about which point in the process you're talking about.

Categories