[Tutor] How to pass command line variables to this python code...

J Cook jcook713 at gmail.com
Tue Jul 14 21:36:40 CEST 2009


Ok,

So I added the following:

<code>
from selenium import selenium
import unittest, time, re
import sys # added this

q = sys.argv[1] # added this
print q # added this just to see

class NewTest(unittest.TestCase):
     def setUp(self):
         self.verificationErrors = []
         self.selenium = selenium("localhost", 4444, "*chrome", 
"http://www.google.com/")
         self.selenium.start()

     def test_new(self):
         sel = self.selenium
         sel.open("/")
         sel.type("q", q) # this is where I want the argument to end up

     def tearDown(self):
         self.selenium.stop()
         self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
     unittest.main()
</code>

I run the following:

$ python selenium-google-test.py yankees
yankees
Traceback (most recent call last):
   File "selenium-google-test.py", line 24, in <module>
     unittest.main()
   File "/usr/lib/python2.6/unittest.py", line 816, in __init__
     self.parseArgs(argv)
   File "/usr/lib/python2.6/unittest.py", line 843, in parseArgs
     self.createTests()
   File "/usr/lib/python2.6/unittest.py", line 849, in createTests
     self.module)
   File "/usr/lib/python2.6/unittest.py", line 613, in loadTestsFromNames
     suites = [self.loadTestsFromName(name, module) for name in names]
   File "/usr/lib/python2.6/unittest.py", line 584, in loadTestsFromName
     parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'yankees'

How do I get the argument over to where I need it to be?

Justin

vince spicer wrote:
> First off, selenium is a great tool and the python driver is very powerful
> 
> there are numerous ways to access cli variables,
> 
> the quickest
> 
> import sys
> print sys.srgv
> 
> sys.argv will it output a array of all command line args
> 
> ./selenium-google-test.py yankees
> will out put:
> 
> ['selenium-google-test.py', 'yankees']
> 
> so
> 
> args = sys.argv
> 
> args[0] == 'yankees'
> True
> 
> for a more functional way, check out
> http://docs.python.org/library/getopt.html
> 
> 
> 
> On Tue, Jul 14, 2009 at 11:11 AM, J Cook <jcook713 at gmail.com 
> <mailto:jcook713 at gmail.com>> wrote:
> 
>     Hello,
> 
>     I have some autogenerated code from Selenium which I cannot figure
>     out how to pass some command line variables to. For example I could
>     export the same in Perl and it would be for example:
> 
>     <code>
>     use strict;
>     use warnings;
>     use Time::HiRes qw(sleep);
>     use Test::WWW::Selenium;
>     use Test::More "no_plan";
>     use Test::Exception;
> 
>     my $sel = Test::WWW::Selenium->new( host => "localhost",
>                                        port => 4444,
>                                        browser => "*chrome",
>                                        browser_url =>      
>     "http://www.google.com/" );
> 
>     $sel->open_ok("/");
>     $sel->type_ok("q", "red sox");
>     </code>
> 
>     I could then go in and add something like:
> 
>     my ($arg1) = shift || "default";
> 
>     which would pick up the first command line parameter and then I
>     could do something like:
> 
>     $sel->(type_ok, $arg1);
> 
>     All is good here, now Selenium will export the following for Python:
> 
>     <code>
>     from selenium import selenium
>     import unittest, time, re
> 
>     class NewTest(unittest.TestCase):
>        def setUp(self):
>            self.verificationErrors = []
>            self.selenium = selenium("localhost", 4444, "*chrome",
>     "http://www.google.com/")
>            self.selenium.start()
> 
>        def test_new(self):
>            sel = self.selenium
>            sel.open("/")
>            sel.type("q", "red sox")
> 
>        def tearDown(self):
>            self.selenium.stop()
>            self.assertEqual([], self.verificationErrors)
> 
>     if __name__ == "__main__":
>        unittest.main()
>     </code>
> 
>     Now I am confused on how to pass a command line parameter here. Any
>     suggestions? I would like to be able to run something like:
> 
>     $ python selenium-google-test.py "yankees"
> 
>     Suggestions?
> 
> 
>     TIA
> 
>     Justin
>     _______________________________________________
>     Tutor maillist  -  Tutor at python.org <mailto:Tutor at python.org>
>     http://mail.python.org/mailman/listinfo/tutor
> 
> 


More information about the Tutor mailing list