How to get (print) the current time inside the watir gem file? - watir

I would like to print the current time the function is called inside the watir gem file.
Eg:
inside /watir/element/select.rb
I need to print the current time, consider when the select_by is called. I tried by adding
require 'time'
..
..
def select_by
puts Time.now
..
But, it is showing error as
`NoMethodError in setup: undefined method `now' for Watir::Time:Class",
"C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/page-object-2.1.1/lib/page-object/platforms/watir/page_object.rb:1068:in `instance_eval'"
"(eval):1:in `process_watir_call'",`

Related

Attempting to append all content into file, last iteration is the only one filling text document

I'm trying to Create a file and append all the content being calculated into that file, but when I run the script the very last iteration is written inside the file and nothing else.
My code is on pastebin, it's too long, and I feel like you would have to see exactly how the iteration is happening.
Try to summarize it, Go through an array of model numbers, if the model number matches call the function that calculates that MAC_ADDRESS, when done calculating store all the content inside a the file.
I have tried two possible routes and both have failed, giving the same result. There is no error in the code (it runs) but it just doesn't store the content into the file properly there should be 97 different APs and it's storing only 1.
The difference between the first and second attempt,
1 attempt) I open/create file in the beginning of the script and close at the very end.
2 attempt) I open/create file and close per-iteration.
First Attempt:
https://pastebin.com/jCpLGMCK
#Beginning of code
File = open("All_Possibilities.txt", "a+")
#End of code
File.close()
Second Attempt:
https://pastebin.com/cVrXQaAT
#Per function
File = open("All_Possibilities.txt", "a+")
#per function
File.close()
If I'm not suppose to reference other websites, please let me know and I'll just paste the code in his post.
Rather than close(), please use with:
with open('All_Possibilities.txt', 'a') as file_out:
file_out.write('some text\n')
The documentation explains that you don't need + to append writes to a file.
You may want to add some debugging console print() statements, or use a debugger like pdb, to verify that the write() statement actually ran, and that the variable you were writing actually contained the text you thought it did.
You have several loops that could be a one-liner using readlines().
Please do this:
$ pip install flake8
$ flake8 *.py
That is, please run the flake8 lint utility against your source code,
and follow the advice that it offers you.
In particular, it would be much better to name your identifier file than to name it File.
The initial capital letter means something to humans reading your code -- it is
used when naming classes, rather than local variables. Good luck!

Why does file.append write "hello world" as duplicate in csv file

When I run the following code:
f = new File(projectroot + "/outputTest1.csv")
f.append("hello world" + '\n')
Here is the result in csv file:
hello world
hello world
Why is it displaying a duplicate?
Take note: I'm using groovy in soapUI
Append adds given string to the end of the file, so when you run it for the third time, it should display 3 hello worlds.
Two possible explanations there :
You're not clearing the file content before writing to it, which means you'll add a new line with "Hello world" every time you execute your code. Either :
use the "Write" method instead of "Append"
clear the file using file.bytes = new byte[0]
You call this piece of code twice, which can be checked by adding a log/display and checking if your log only appears once.
Further reading :
SO question about file clearing
Groovy documentation about file handling
I was using this code inside of an assertion of SoapUI then created duplicate. When I added a separate groovy step with the same code then it worked correctly.

How to run a string from an input file as python code?

I am creating something along the likes of a text adventure game. I have a .yaml file that is my input. This file looks something like this
node_type:
action
title:
Do some stuff
info:
This does some stuff and things
script:
'print("hello world")
print(ret_val)
foo.bar(True)
ret_val = (foo.bar() == True)
if (thing):
print(thing)
print(ret_val)
'
My end goal is to have my python program run the script portion of the yaml file exactly as if it had been copy pasted into the main code. (I know there are about ten bazillion security reasons I should not be running user input like this, but I am the only one writing these nodes, and the only one using this program so I'm mostly just ignoring this fact...)
Currently my attempt goes like this: I load my yaml file as a dict using pyyaml
node = yaml.safe_load(file.yaml)
Then I'm trying to use exec to run my code and hitting a lot of problems, I can't run if statements, I simply get a syntax error, and I can't get any sort of return value from my code. I've tried this as a work around:
def main()
ret_val = "test";
thing = exec(node['script'], globals(),locals())
print(ret_val)
which when run with the above .yaml file prints
>> hello world
>> test
>> True
>> test
for some reason not actually modifying any of my main variables even though I fed them to exec.
Is there any way for me to work around these issues or is there an all together better way to be doing this?
One way of doing this would be to parse the code out and save it to a .py file, from which it can be imported dynamically, for example by importlib.
You might want to encapsulate parsed code into a function, which you can then easily call to invoke your action. Also, it would make sense to specify some default imports there.

How to use machine-generated variables in cookiecutter

Is there a way to machine-generate some values, after the user has supplied some their values for the variables in cookiecutter.json?
The reason I ask is that:
one of the values I need to prompt for is rather hard for users to work out
but it's really easy for me to write some Python code to generate the right value
So I'd really like to be able to remove the user prompt, and calculate the value instead.
Things I've tried:
Searched online for an example pre_gen_project.py file to show how to do it
Read the cookiecutter Advanced Usage page
I'm using cookiecutter on the command line:
cookiecutter path_to_template
Am I missing any tricks?
I needed this exact capability just a few days ago. The solution I came up with was to write a wrapper script for cookiecutter, similar to what is mentioned in:
http://cookiecutter.readthedocs.io/en/latest/advanced_usage.html#calling-cookiecutter-functions-from-python
My script generates a random string for use in a Django project. I called my script cut-cut:
#! /usr/bin/env python
from cookiecutter.main import cookiecutter
import os
rstring = ''.join([c for c in os.urandom(1024)
if c.isalnum()])[:64]
cookiecutter(
'django-template', # path/url to cookiecutter template
extra_context={'secret': rstring},
)
So now I simply run cut-cut and step through the process as normal. The only difference is that the entry named secret in my cookiecutter.json file is prepopulated with the generated value in rstring from the script, provided via the extra_context passed.
You could modify the script to accept the template via the command line, but in my usage I always use the same template, thus I simply pass a hard coded value "django-template" as noted in the code above.

Watir::Wait.until not working with frames

I'm automating an internal tool that is rife with frames, using Watir. I am able to manipulate all the various elements so I know I am identifying the frames correctly, but any time I attempt to use a Wait statement for any of these elements it fails. Tracking back through the error message, it always hits the activesupport gem in core_ext/time/calculations and it looks like it can't get the duration value, it gets set to false, and then the operation fails because it is expecting a Float. Is this a bug?
Using Ruby 1.8.7 and Watir 1.6.7
My code is:
require 'rubygems'
require 'watir/testcase'
require 'main_setup'
require 'win32ole'
require 'common'
class Smoketest < Watir::TestCase
include CommonCode
def test_AddEdit_Endpoint
Watir::Wait.until { #b.link(:id,"lbShowEndpointForm").exists? }
end
end
Error is the following:
test_basic_smoke(Smoketest):
TypeError: can't convert false into Float
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/core_ext/time/calculations.rb:278:in `plus_without_duration'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/core_ext/time/calculations.rb:278:in `+'
C:/Ruby187/lib/ruby/gems/1.8/gems/commonwatir-1.6.7/lib/watir/wait.rb:15:in `until'
C:/qa/trunk/CCAdmin/Automation/CCAdmin/lib/smoketest.rb:27:in `test_basic_smoke'
So, which line is the C:/qa/trunk/CCAdmin/Automation/CCAdmin/lib/smoketest.rb:27?
I thought the correct usage for the command was wait_until, unless it's changes from Watir 1.6.5, http://wtr.rubyforge.org/rdoc/1.6.5/classes/Watir/Waiter.html

Resources