10

I had written a scipt in Java with Webdriver and it worked fine and below is the code for the sample

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.Selenium;
import java.util.*;
import java.lang.Thread.*;

public class Login {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }

 @Before
 public void setUp() throws Exception {
 }

 @After
 public void tearDown() throws Exception {
 }

    public static void main(String[] args) {
         WebDriver driver = new FirefoxDriver();
         Selenium selenium = new WebDriverBackedSelenium(driver,     "http://192.168.10.10:8080/");
         selenium.open("/");
   selenium.keyPress("name=user_id", "admin");
   }
     }

}

But my requirement is to implement the same in python with webdriver, can you please let me know how this can be done with the above example and webdriver binaries and how to do setup for the same

3 Answers 3

12

Did you read the instructions at python bindings for WebDriver ?

example2.py is clear, although not a direct translation of your code:

import unittest
from google_one_box import GoogleOneBox
from selenium.firefox.webdriver import WebDriver

class ExampleTest2(unittest.TestCase):
    """This example shows how to use the page object pattern.

    For more information about this pattern, see:
    http://code.google.com/p/webdriver/wiki/PageObjects
    """

    def setUp(self):
        self._driver = WebDriver()

    def tearDown(self):
        self._driver.quit()

    def testSearch(self):
        google = GoogleOneBox(self._driver, "http://www.google.com")
        res = google.search_for("cheese")
        self.assertTrue(res.link_contains_match_for("Wikipedia"))

if __name__ == "__main__":
    unittest.main()

A test module, GoogleOneBox, models a page that has a google search bar (the url moved a little).

Sign up to request clarification or add additional context in comments.

3 Comments

From where I can get the Webdriver for Python and what are the installation guidelines. And what is the use of the package - GoogleOneBox ?
Follow instructions on code.google.com/p/selenium/wiki/PythonBindings - they provide download options. Googleonebox models a page that has a google search bar. It is a part of the test suite, code.google.com/p/selenium/source/browse/trunk/common/test/py/…
3 years down the road, the test module is at code.google.com/p/selenium/source/browse/py/test/selenium/…
2
import unittest
from selenium import webdriver

class Login(unittest.TestCase):
    def setUp(self):
       self.driver = webdriver.Firefox()

    def tearDown(self):
       self.driver.quit()

    def test_login(self):
       driver = self.driver
       driver.get('http://testurl')
       username = driver.find_element_by_name('user_id')
       username.send_keys('admin')

Comments

1

this is a plug, but maybe answers your question a bit:

import unittest
from holmium.core import PageObject, PageElement, PageElements, Locators

class GoogleMain(PageObject):
    search_box = PageElement( Locators.NAME, "q", timeout = 1)
    search_results = PageElements( Locators.CSS_SELECTOR, "li.g", timeout = 1)

    def search ( self, query ):
        self.search_box.clear()
        self.search_box.send_keys(query)
        self.search_box.submit()

class Test(unittest.TestCase):
    def test_search_simple(self):
        self.assertTrue(
                len( GoogleMain(self.driver, "http://google.com").search( "selenium" ).search_results) > 0
        )

details at holmium.core documentation holmium.core documentation

run as:

nosetests test_google.py --with-holmium --holmium-browser=firefox 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.