Write a monitoring tool with selenium+java - java

Good day!
I have an idea about using selenium as short-time monitoring tool. For example, need to check for an two-three hours about some table values changing.
I've got in mind a cycle "while", where i set up timer how long need to monitor values, and then print them for easy compare.
2016.04.26 | 160789 186491 0.76% 05:28:56
2016.04.26 | 160789 186491 0.76% 05:30:56
But I think there is better, smart solution. But i can't figure out how.
open(projectUrl);
int timer = 120;
int i = 1;
int iterations = 50;
String var1 = $("cssSelector1").getText();
while (i<iterations) {
open(projectUrl);
var1 = $("cssSelector1").getText();
if (!$("cssSelector1").getText().equals(var1)) {
System.out.print(
var1+" | "+
$("cssSelector2").getText()+" "+
$("cssSelector3").getText()+" "+
$("cssSelector4").getText()+" ");
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("hh:mm:ss");
System.out.println(ft.format(dNow));
}
sleep(timer*1000);
i++;
}
Now it's done, and works like I want. When var1 changes, update var1, then write value. And cycling again. Upper code works fine.

var1..4 need to be set within the while loop or they'll just keep printing the same data through each iteration

Now it's done, and works like I want. When var1 changes, update var1, then write value. And cycling again. Upper code works fine.

Related

Comparing values obtained from combo box to another string fails even though both appear to be the same

I am trying to obtain a date from 3 separate combo boxes that I then convert to ints and make a Date object. However, when I compare this date string to an already existing string, it doesnt match even though in the debugger it appears to be the same. I have setup a simple if statement to check what the problem is however I am not sure why it does not match.
int apptDay, apptMonth, apptYear;
apptDay = Integer.parseInt(consultationDay.getSelectedItem().toString());
apptMonth = Integer.parseInt(consultationMonth.getSelectedItem().toString());
apptYear = Integer.parseInt(consultationYear.getSelectedItem().toString());
consultationDate = new Date(apptDay, apptMonth, apptYear);
if (appointmentList.get(0).getDate() == consultationDate) {
sopl("Working");
}
I am quite sure that there is some issue with my code related to combo boxes as that is the only place where I face problems. The if statement is never satisfied so "Working" is never printed.
Any help would be appreciated.
You should use .equlas() instead of the == operator.
int apptDay = 5, apptMonth = 12, apptYear = 2000;
Date testDate = new Date(apptDay, apptMonth, apptYear);
Date consultationDate = new Date(apptDay, apptMonth, apptYear);
if (testDate == consultationDate) {
System.out.println("Success for ==");
} else if (testDate.equals(consultationDate)) {
System.out.println("Success for equals");
}

Add float to Java Calendar

I'm currently developing some functionality that needs to either subtract or add time to a Calendar class instance. The time I need to add/sub is in a properties file and could be any of these formats:
30,sec
90,sec
1.5,min
2,day
2.333,day
Let's assume addition for simplicity. I would read those values in a String array:
String[] propertyValues = "30,sec".split(",");
I would read the second value in that comma-separated pair, and map that to the relevant int in the Calendar class (so for example, "sec" becomes Calendar.SECOND, "min" becomes Calendar.MINUTE):
int calendarMajorModifier = mapToCalendarClassIntValues(propertyValues[1]);
To then do the actual operation I would do it as simple as:
cal.add(calendarMajorModifier, Integer.parseInt(propertyValues[0]));
This works and it's not overly complicated. The issue is now floating values (so 2.333,day for eaxmple) - how would you deal with it?
String[] propertyValues = "2.333,day".split(",");
As you can imagine the code becomes quite hairy (I haven't actually written it yet, so please ignore syntax mistakes)
float timeComponent = Float.parseFloat(propertyValues[0]);
if (calendarMajorModifier == Calendar.DATE) {
int dayValue = Integer.parseFloat(timeComponent);
cal.add(calendarMajorModifier, dayValue);
timeComponent = (timeComponent - dayValue) * 24; //Need to convert a fraction of a day to hours
if (timeComponent != 0) {
calendarMajorModifier = Calendar.HOUR;
}
}
if (calendarMajorModifier == Calendar.HOUR) {
int hourValue = Integer.parseFloat(timeComponent);
cal.add(calendarMajorModifier, hourValue);
timeComponent = (timeComponent - hourValue) * 60; //Need to convert a fraction of an hour to minutes
if (timeComponent != 0) {
calendarMajorModifier = Calendar.MINUTE;
}
}
... etc
Granted, I can see how there may be a refactoring opportunity, but still seems like a very brute-forceish solution.
I am using the Calendar class to do the operations on but could technically be any class. As long as I can convert between them (i.e. by getting the long value and using that), as the function needs to return a Calendar class. Ideally the class also has to be Java native to avoid third party licensing issues :).
Side note: I suggested changing the format to something like yy:MM:ww:dd:hh:mm:ss to avoid floating values but that didn't pan out. I also suggested something like 2,day,5,hour, but again, ideally needs to be format above.
I'd transform the value into the smallest unit and add that:
float timeComponent = Float.parseFloat(propertyValues[0]);
int unitFactor = mapUnitToFactor(propertyValues[1]);
cal.add(Calendar.SECOND, (int)(timeComponent * unitFactor));
and mapUnitToFactor would be something like:
int mapUnitToFactor(String unit)
{
if ("sec".equals(unit))
return 1;
if ("min".equals(unit))
return 60;
if ("hour".equals(unit))
return 3600;
if ("day".equals(unit))
return 24*3600;
throw new InvalidParameterException("Unknown unit: " + unit);
}
So for example 2.333 days would be turned into 201571 seconds.

Incrementing a single parameter in an array of objects

I am trying to get the hour component of an object, which is an int, and increment it by 1. So 5 would be 6. I tried to do clocks[i].getHour() = clocks[i].getHour()+1, but this was not allowed and would say I need a variable on the left.
public void daylightSavingsTime(Clock[] clocks) {
for(int i = 0; i <clocks.length; i++) {
int a = clocks[i].getHour()+1;
}
Probably you'd need to write/use a setter function setHour(int newHour) ,
and then you could do something along the lines of:
public void daylightSavingsTime(Clock[] clocks) {
for(int i = 0; i <clocks.length; i++) {
clocks[i].setHour(clocks[i].getHour()+1);
}
Explanation of your statement.
clocks[i].getHour() = clocks[i].getHour()+1;
//To simplify
Clock clock = clocks[i];
clock.getHour() = clock.getHour()+1; //thing you are trying to do.
Suppose clock hours field is 6. What the compiler will do. When it sees clock.getHours() it knows the value is 6. To compiler your statement looks like
6 = 6+1
Obviously no language wants to change the value of 6 to 7
But int a = clocks[i].getHour()+1; is a valid statement as a will be assigned to 7.
To increment the hour in clock just use just do
clock.hours = clocks[i].getHour()+1;
but now hours property is public and can be easily abused like clock.hours = 100; but we may not want it to be 100, but to increase the date as 100 > 24. So use set method like clock.setHour(a+1) this method will internally handle the hour like
Class Clock{
// like that.. it may be different..
void setHour(int hour){
this.day += hour/24;
this.hour += hour%24;
}
}

Code crashing after using formatting

I'm new to the Java language (Just started about 2 weeks ago)
Basically, the user enters their year/month/day they were born on in order and I use this information to perform a math calculation that will show their age.
I need numbers from 0-9 to be taken in as 01, 02, 03... So, I searched around and found that I can use Decimal.Format and then print out the format later on.
My code crashes whenever it reaches the println(twodigits.format) part no mater where I put it. There are no errors displayed that I need to address.
Why is it doing this and is there a better way to do this? I need it to be 2 digits at all times or the calculation won't work.
Here's a part of my code, I can provide more if needed.
DecimalFormat twodigits = new DecimalFormat("00");
System.out.println("Calculating...");
Integer CurrentDate2 = Integer.valueOf(CurrentDate);
Integer BirthDate2 = Integer.valueOf(BirthDate);
int a = CurrentDate2.intValue();
int b = BirthDate2.intValue();
int age = (a - b) / 1000;
Thread.sleep(300);
System.out.println(".");
Thread.sleep(300);
System.out.println(".");
Thread.sleep(300);
System.out.println(".");
System.out.println(twodigits.format(CurrentDate));
System.out.println(twodigits.format(BirthDate));
Any help is appreciated!
What types are "CurrentDate" and "BirthDate" because it's not clear from your code? You first use them to set "CurrentDate2" and "BirthDate2". And then you use them in the println().
If I were to guess, I'd say they are of type 'String', and 'twodigits.format()' can't handle Strings, which is why it's crashing.
This takes two dates and split time on "/". It then prints them out in the format that you want.
DecimalFormat twodigits = new DecimalFormat("00");
System.out.println("Calculating...");
String CurrentDate = "01/02/2007";
String BirthDate = "02/03/2007";
String[] currentDateParts = CurrentDate.split("/");
String[] birthDateParts = BirthDate.split("/");
int cdp0 = Integer.parseInt(currentDateParts[0]);
int cdp1 = Integer.parseInt(currentDateParts[1]);
int cdp2 = Integer.parseInt(currentDateParts[2]);
int bdp0 = Integer.parseInt(birthDateParts[0]);
int bdp1 = Integer.parseInt(birthDateParts[1]);
int bdp2 = Integer.parseInt(birthDateParts[2]);
//do your calculations
System.out.println(twodigits.format(cdp0));
System.out.println(twodigits.format(cdp1));
System.out.println(twodigits.format(cdp2));
System.out.println(twodigits.format(bdp0));
System.out.println(twodigits.format(bdp1));
System.out.println(twodigits.format(bdp2));

How to get how much space a Method or a return value used?

Is it possible to know/get how much space/memory are used when a method is executed or when there is a return value? I don't want to know how much space used by the app, just some code like method or the return value. I tried the runtime.getRuntime, but from my understanding, It looks like it tells me how much space is used by the entire code/app, am I right?
EDIT :
public int [] randtotal(int times2)
{
int in1[] = new int[times2];
for (int i = 0; i<Num2; i++)
Random rand = new Random();
{
in1[times2]= rand.NextInt(5);
}
totalNum(in1);
return int1;
}
As you can see here, at the end of the code there is the "return int1;" , so I want to know when these code is executed how much space is allocated for the value here?
You can use Visualvm tool to profile the overall application and you can also profile certain package, class or function
see this link :
https://visualvm.java.net/profiler.html

Categories