Skip to main content

Selenium Python Interview Questions

Displaying 1 - 10 of 10

Selenium with Python: What are different explicit conditions which can be used with explicit wait?

Selenium Python binding provides some convenience methods so you don’t have to code an expected_condition class yourself or create your own utility package for them.

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable
  • staleness_of
  • element_to_be_selected
  • element_located_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present

What are different types of waits in Selenium with Python?

Selenium WebDriver provide 2 types of waits:

1. Implicit Wait

2. Explicit Wait

Implicit Wait: An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object. Example:

driver.implicitly_wait(10) # seconds

Explicit Wait: An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code.

element = WebDriverWait(driver, 10).until(
        expected_conditions.presence_of_element_located((By.ID, "myDynamicElement"))

Other Techniques: There are other techniques to wait for page load and stop a program execution for certain amount of time.

driver.set_page_lot_timeout(1);
time.sleep(2)

In above code exception will be thrown if page does not load in 1 seconds. time.sleep is used to stop the program execution for 2 seconds.

How to skip a test case based on condition using PyTest?

If you wish to skip something conditionally then you can use skipif instead. Here is an example of marking a test function to be skipped when run on an interpreter earlier than Python3.6:

@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_function():
   ...

If the condition evaluates to True during collection, the test function will be skipped, with the specified reason appearing in the summary when using -rs.

How to skip test case using PyTest?

 The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason:

@pytest.mark.skip(reason="this will not execute")
def mytest():
  ...

Alternatively, it is also possible to skip imperatively during test execution or setup by calling the pytest.skip(reason) function:

def my_function():
    if not valid_config():
        pytest.skip("unsupported configuration")

How to include or exclude tests for execution based on their name using PyTest?

 You can use the -k command line option to specify an expression which implements a substring match on the test names instead of the exact match on markers that -m provides. This makes it easy to select tests based on their names:

def mytest1():
    pass

def mytest2():
    pass
pytest -v -k mytest1

To exclude test case using not keyword inside double quotes. Below code will execute mytest2 not mytest1.

pytest -k "not mytest1" -v

You can use or keyword to select multiple test cases matching name or keyword. Below code will execute both test cases.

pytest -k "mytest1 or mytest2" -v

How to tag and execute test cases with smoke or regression using PyTest?

 You can mark a test function using custom meta data:

@pytest.mark.smoke
def mytest1():
    pass  # perform some web test test for your app

@pytest.mark.regression
def mytest1():
    pass  # perform some web test test for your app

In above code smoke and regression are tag name.

You can then restrict a test run to only run tests marked with smoke or regression:

pytest -v -m smoke

How to use Assert in PyTest?

 Assertions are checks that return either True or False status. In pytest, if an assertion fails in a test method, then that method execution is stopped there. The remaining code in that test method is not executed, and pytest will continue with the next test method.

Below are few examples of Asserts:

assert "hello world" == "hello"
assert 10 == 10
assert driver.title == "Login Page"
assert driver.current_url = "https://www.programsbuzz.com"

How to execute set of code before or after each test case using PyTest.

 You can use PyTest Fixture for this. In below example, code before yield will execute before every test case and code after yield will execute after every test case.

@pytest.fixture()
def fix_method()
    global driver
    path = ""
    driver = Chrome(executable_path = path)
    driver.get("");
    yield
    driver.close()

What is PyTest Fixture?

 If you want to execute set of code before or after your test you can use PyTest Fixture. 

The purpose of test fixtures is to provide a fixed baseline upon which tests can reliably and repeatedly execute. pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions:

  • fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects.
  • fixtures are implemented in a modular manner, as each fixture name triggers a fixture function which can itself use other fixtures.
  • fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component options, or to re-use fixtures across class, module or whole test session scopes.

Example:

from selenium.webdriver import Chrome
import pytest

@pytest.fixture()
def fix_method()
    global driver
    path = ""
    driver = Chrome(executable_path = path)
    driver.get("");
    yield
    driver.close()

def test_1(fix_method):
	...
	...

def test_2(fix_method):
	...
	...

def test_3(fix_method):
	...
	...

In above scenario, code in fix_method will be executed before all 3 Test cases except the one line after yield. Code after yield will execute after every test case. Don't forget you need to include fix_method name as a parameter in all test.

If you want to execute fixture code only once in module, you can specify its scope.

@pytest.fixture(scope="module")
def fix_method()
    global driver
Subscribe to Selenium Python Interview Questions

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.