3

I'm new to Python and selenium. I need to write a login module that I can reuse in my test cases. Here's my 2 files. I need help to call the login module so that the browser launches and user can login. Then the second module starts and test case begins (in the same browser). I have written 2 class in 2 separate files. My code is below:

mylogin.py file

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
import unittest
class myLoginclass(unittest.TestCase):
    @classmethod   
    def test_TC_1_login_page(self):       
        self.driver = webdriver.Firefox()
        self.driver.get(http://www.gmail.com)
        self.driver.find_element_by_xpath(".//*[@id='name-group']/input").send_keys("HELLO")
        self.driver.find_element_by_xpath(".//*[@id='password-group']/input").send_keys("WORLD")
        self.driver.find_element_by_id("loginButton").click()

if __name__ == '__main__':
    unittest.main(failfast=True, exit=False)

The myorder.py file:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
import unittest
from mylogin import myLoginclass
class myorderclass(unittest.TestCase):
    @classmethod   
    def test_TC_2_orderProcess(self):       
        self.driver.find_element_by_xpath("".//*[@id='aoTkt']/div/div")).click()
        self.driver.find_element_by_xpath(".//*[@id='presales']/input").click()
        self.driver.find_element_by_link_text("DISPATCH").click()
        self.driver.find_element_by_id("submitButton").click()

if __name__ == '__main__':
    unittest.main(failfast=True, exit=False)
ZF007
  • 3,708
  • 8
  • 29
  • 48
Nisheeth
  • 271
  • 4
  • 13
  • Can you fix the formatting of your code? – G_M Mar 14 '18 at 22:39
  • fixed the indentation issues in my sample code – Nisheeth Mar 14 '18 at 23:35
  • In `self.driver.find_element_by_xpath("".//*[@id='aoTkt']/div/div")).click()` you have "("...."))".().. which means you got one ")" bracket too much in your code. Typo? – ZF007 Mar 14 '18 at 23:45
  • yes, extra ) it's a typo . But the question is how to use the browser invoked from one class be used to continue with the next test case, which is written in another class. – Nisheeth Mar 15 '18 at 01:25

1 Answers1

0

Use class inheritance. Call the login function with either setUp() or setUpClass() depending on your use case.

from mylogin import myLoginclass

class myorderclass(myLoginclass):
    def setUp(self):
        self.test_TC_1_login_page()

Unittest setUp/tearDown for several tests

Sahil Agarwal
  • 555
  • 3
  • 12
  • 1
    This solution partially works. The myorder class runs, which includes myloginclass followed by myorder class. But after this, myloginclass executes again, opening a new browser and login. – Nisheeth Mar 15 '18 at 16:10