Selenium Date comparison example - selenium

I am trying to validate dates using Selenium. So my scenario is;
Scenario: Date validation
When I set field <field> to value <value>
And I save the form
Then I should see error message <message>
Examples:
| field | value | message |
| startDate | 01/01/2014 | End date should be greater than start date |
| endDate | 01/01/2014 | End date should be greater than start date |
My step method to populate field is generic as below;
#When("I set field <field> to value <value>")
public void populateField(#Named("field") String fieldName, #Named("value") String value) {
populateFieldValue(fieldName, value);
}
My question is since the step method is generic, how do I set the start date, then end date and display the error message in a single example line.

You can use a StringListConverter to pass lists of values to parameters
Simple example:
Scenario: Date validation
When I set field <field> to value <value>
Then I should see error message <message>
Examples:
| field | value | message |
| startDate | 01/01/2014 | End date should be greater than start date |
| endDate | 01/01/2014 | End date should be greater than start date |
| startDate,endDate | 01/01/2014, 01/01/2014 | display the error message in a single example line. |
And a test code:
#When("I set field <field> to value <value>")
public void xxxx(#Named("field") List<String> fields,
#Named("value") List<String> values){
System.out.println(">>>>>------------->>>>----------->>>>>");
for( int i = 0; i < fields.size(); i++ ){
String field = fields.get(i);
String value = values.get(i);
System.out.println(" --> Set field: " + field + " to value: " + value);
}
}
#Then("I should see error message <message>")
public void when(#Named("message") String message){
System.out.println(" -------> message = " + message);
}
The above test generates this results:
Running story main/resources/test.story
>>>>>------------->>>>----------->>>>>
--> Set field: startDate to value: 01/01/2014
-------> message = End date should be greater than start date
>>>>>------------->>>>----------->>>>>
--> Set field: endDate to value: 01/01/2014
-------> message = End date should be greater than start date
>>>>>------------->>>>----------->>>>>
--> Set field: startDate to value: 01/01/2014
--> Set field: endDate to value: 01/01/2014
-------> message = display the error message in a single example line.

Related

How do i get the Specflow scenario outline example data to a table

Is there any way to get the scenario context outline example values i mean all the values in to a table
Scenario Outline: Create a Matter
Given I enter "< parameter1 >"
Then I enter "<parameter2>"
Then I enter "<parameter3>"
Then I enter "<parameter4>"
Then review all the parameters entered above in this final step
Examples:
| parameter1 | Paramter2|Parameter3|Parameter4|....|parameter14|
| value |value2 |value3 |value4 |....|value14|
in the above scenario is there any way to get all the example values in step4 to a table
I know I can set ScenarioContext.Current[parameter1] = value in each step
In my case I have 14 parameters which are used in each step but in the final step i need to use all the 14 parameters
is there any way I get the example values in to table.
I don't want to break in to smaller scenario
like below
Scenario: breaking in to smaller chunks
Given I enter the following
| parameter1 | Paramter2|
| value |value2|
Here is something I use that may help. Andreas is the expert though on this stuff and he probably has a better idea. Since your format was less than ideal, I used a basic scenario.
Change it to a "Scenario" and Drop the "Scenario Outline".
The feature looks like this:
Scenario: Validate Shipping Fees
When the user enters the State then we can verify the city and shipping fee
| City | State | Shipping |
| Boulder | Colorado | 6.00 |
| Houston | Texas | 8.00 |
Add the Table.
public class ShippingTable
{
public string City { get; set; }
public string State { get; set; }
public string Shipping { get; set; }
}
Then in your step:
[When(#"the user enters the State then we can verify the city and shipping fee")]
public void WhenTheUserEnterTheStateThenWeCanVerifyTheCityAndShippingFee(Table table)
{
var CityState = table.CreateSet<ShippingTable>();
foreach (var row in CityState)
{
try
{
Pages.CheckoutPage.SelectState(row.State);
Pages.CheckoutPage.SelectCity(row.City);
var recdPrice = Pages.CheckoutPage.GetShippingPrice;
Assert.AreEqual(row.shipping, recdPrice);
}
catch (Exception)
{
throw new Exception("This is jacked up");
}
}
}

How to display the dynamic values in cucumber extent report

i want to show the dynamic values in cucumber extent report
Ex:
Feature file
Given enter the userid as <"userid">
And enter the current time as <"currenttime">
Examples:
|userid | currenttime|
|10002 | today |
Definition file:
#And("^enter the current time as (.*)")
public void currenttime(String arg1) {
SimpleDateFormat formatter = new SimpleDateFormat("HH:MM");
Date date = new Date();
System.out.println(formatter.format(date));
if (arg1 == "today") {
driver.findElement(By.id("period")).sendKeys(formatter.format(date));
}
}
In cucumber Extent Report: it shows "today" only instead of dynamic value (current time= "07:18")
please guide me..

Finding index using switch case statement in javascript

I'm using Pentaho(ETL) tool to achieve the output using a javascript component which accepts javascript code to achieve the desired transformation.The following table is imported into pentaho from a .csv file(source file).
For example this is my table structure
+--------+--------+--------+
| RLD | MD | INC |
+--------+--------+--------+
| 0 | 3868 | 302024 |
| 53454 | 7699 | 203719 |
| 154508 | 932 | 47694 |
| 107547 | 36168 | 83592 |
I want to use a script which would give me the max_value and its index number, such that my output would look like
Output Table
+--------+--------+--------+-----------+-----------+
| RQD | MT | IZC | max_value | max_index |
+--------+--------+--------+-----------+-----------+
| 0 | 3868 | 302024 | 302024 | 3 |
| 53454 | 7699 | 203719 | 203719 | 3 |
| 154508 | 932 | 47694 | 154508 | 1 |
| 456 | 107547| 83592 | 107547 | 2 |
To get the max value from rows I have used
var max_value = Math.max(RQD,MT,IZC);
println(max_value);
I tried to get their index using the following script
var max_index = switch (Math.max(RQD,MT,IZC))
{
case "RQD":document.write("1")
case "MT":document.write("2")
case "MT":document.write("3")
default:document.write("0")
}
How can I get the desired result in the form of javascript data structure? Any help would be much appreciated.Thanks
var list = [
{RLD:0,
MD:3868,
INC:302024
},
{RLD:53454,
MD:7699,
INC:203719
},
{RLD:154508,
MD:932,
INC:47694
},
{RLD:107547,
MD:36168,
INC:83592
},
];
list = list.map(function(item){
var keys = Object.keys(item);
item.max_value = item[keys[0]];
item.max_index = '';
for(var i = 1, l = keys.length; i < l; i++) {
var key = keys[i];
var keyValue = item[key];
if (item.max_value < keyValue){
item.max_value = keyValue;
item.max_index = key;
}
}
return item;
})
There are several issues with your code, lets solve them!
Use breaks: you must use breaks in order to avoid the switch moving to cases below its match.
switch cases do not return a value like functions, you cannot use a switch return to define a variable, you need to define the variable inside the switch case.
Math.max does not return the name of its maximum variable, instead it returns the maximum number from its given parameters.
to solve this issue, i would not use a switch case with math.max to be honest, however to answer your question:
var tableArray = [RQD,MT,IZC];
var maxIndex = tableArray.indexOf(Math.max.apply(null, arr));
if(maxIndex > -1) document.write(maxIndex+1);
i used +1 because you have your index in the example table starting from 1 instead of 0.
the way the array is sorted should match the way the table is sorted per raw.
First of all you could not solve this problem with a switch statement.
In a javascript switch you should provide a value that is one of the followed case(s), otherwise the switch will go to the default if defined.
Your problem seems to be to find out the higher value of 3 columns and print out, the colums row by row adding a column with the max value and the index of the column where you found it.
So for example on the row:
1, RLD : 0
2, MD : 3868
3, INC : 302024
In this case the higher value is INC with the column index 3.
If you have just the variables with the number values, you could do nothing more than something like this:
function getMaxValueRow (RLD, MD, INC) {
var max_value = RLD;
var max_index = 1;
if (MD > max_value) {
max_value = MD;
max_index = 2;
}
if (INC > max_value) {
max_value = INC;
max_index = 3;
}
return [RLD, MD, INC, max_value, max_index];
}
You could return an object too like this:
retrun {
'RQD': RLD,
'MT': MD,
'IZC': INC,
'max_value': max_value,
'max_index': max_index
}

JDBC single row resultset access error

I have a single row query output loks like below
Product | Process | s_int
--------------------------
W01 | AP_03 | 1
To access the data I am using result set (rSet)
rSet.first();
do{
product = rSet.getString("PRODUCT");
process = rSet.getString("PROCESS");
s_int = rSet.getInt("S_INT");
System.out.println("Hello Product["+rSet.getString("PRODUCT")+"] Process["+rSet.getString("PROCESS")+"] S_Int["+rSet.getInt("sint")+"]");
}while(rSet.next());
I am getting this error:
The result set has no current row
and the returns are null and 0.
What I am doing wrong?
ResultSet.first() returns a boolean indicating if it is on a row or not:
true if the cursor is on a valid row; false if there are no rows in the result set
Check this before attempting to access the ResultSet. An alternate structure to code:
//final ResultSet rSet = stmt.executeQuery("...");
while (rSet.next())
{
}

org.springframework.jdbc.BadSqlGrammarException: bad SQL grammar

I am getting the following error in my code:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad
SQL grammar [insert into bulletins (date, name, subject, note, approved) values
(?, ?, ?, ?, ?)]; nested exception is com.mysql.jdbc.exceptions.MySQLSyntaxError
Exception: Unknown column 'date' in 'field list'
This line is in my Spring controller.
bulletinDAO.writeBulletin(bulletin);
The actual place in my DAO class where I'm trying to write using Hibernate.
public void writeBulletin(Bulletin bulletin) {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(bulletin);
tx.commit();
} catch (Exception e) {
System.out.println(e.toString());
}
}
Here is my model class.
#Entity
#Table(name="login")
public class Bulletin {
#Id
#Column(name="id")
#GeneratedValue
private int id;
#Column(name="bulletin_date")
private String date;
#Column(name="name")
private String name;
#Column(name="subject")
private String subject;
#Column(name="note")
private String note;
#Column(name="approved")
private boolean approved;
// Getters and setters follow
}
Finally, here is the layout of the table.
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| bulletin_date | varchar(10) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| subject | varchar(50) | YES | | NULL | |
| note | varchar(2500) | YES | | NULL | |
| approved | tinyint(1) | YES | | NULL | |
+---------------+---------------+------+-----+---------+----------------+
There must be something wrong with your getters and setters.
I would recommend changing the property name from date to bulletinDate. And then set & get it correctly...
#Column(name="bulletin_date")
private String bulletinDate;
public String getBulletinDate() {
return bulletinDate;
}
public void setBulletinDate(String bulletin_date) {
this.bulletinDate = bulletin_date;
}
Your issue is here
[insert into bulletins (date, name, subject, note, approved)]
Whereas you need bullletin_date.
From experience i needed to rebuild the project completely to ensure that right column name is referenced.
Clear your project cache and rebuild it.
Let me know how you go and i'll help further if it doesn't help.