Interview Details:
- Position Title: Test Engineer
- Interview schedule month: June – 2023
- Mode of Interview: Microsoft Teams
- Skills: Python, Selenium and Pytest
- Experience Required: 3+
Interview Questions and Answer for the position of Test Engineer
- What is Abstraction?
- What is Polymorphism?
- What is Method overloading and overriding?
- What is Webdriver in selenium?
- Print following output using Python?
*
**
***
****
***** - Open the Ajio website, search jeans in the search box and fetch the brand name, price and original price of the first 6 records.
Q1.What is Abstraction?
-Abstraction is a fundamental concept in programming that involves simplifying complex systems by breaking them down into smaller, more manageable parts by hiding unnecessary details. In Python, abstraction is achieved through classes and objects by exposing only the required attributes and methods to the user while encapsulating the internal implementation details.
Example:
Let’s consider the example of a TV remote control to explain the abstraction concept.
from abc import ABC, abstractmethod # Step 1: Identify the Concept # Define an abstract class representing a TV remote control class RemoteControl(ABC): @abstractmethod def power(self): pass @abstractmethod def change_channel(self, channel): pass @abstractmethod def volume_up(self): pass @abstractmethod def volume_down(self): pass # Step 2: Concrete Remote Control Implementation # Create a concrete implementation of the TV remote control class TVRemoteControl(RemoteControl): def power(self): return "TV is now powered on" def change_channel(self, channel): return f"Changed to channel {channel}" def volume_up(self): return "Volume increased" def volume_down(self): return "Volume decreased" # Step 3: Using Abstraction # Create an instance of the TV remote control and use its methods remote = TVRemoteControl() print(remote.power()) print(remote.change_channel(5)) print(remote.volume_up()) print(remote.volume_down())
Q2.What is Polymorphism?
-Polymorphism in OOP allows objects of different classes to be treated as instances of a common superclass, enabling flexible interactions without knowing specific types. It’s achieved through method overriding and overloading.
Example
Let’s consider the example of Animal sounds to explain the abstraction concept.
class Animal: def speak(self): pass class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" class Duck(Animal): def speak(self): return "Quack!" def make_animal_speak(animal): print(animal.speak()) dog = Dog() cat = Cat() duck = Duck() make_animal_speak(dog) # Outputs: Woof! make_animal_speak(cat) # Outputs: Meow! make_animal_speak(duck) # Outputs: Quack!
Q3.What is Method overloading and overriding?
Method Overloading: Method overloading allows a class to define multiple methods with the same name but different parameters. These methods are distinguished by the number or types of parameters they accept. The method that gets invoked depends on the arguments passed during the method call. In Python, method overloading is achieved through default argument values or using the *args
and **kwargs
constructs.
#Example of Method Overloading class MathOperations: def add(self, a, b): return a + b def add(self, a, b, c): return a + b + c math_obj = MathOperations() print(math_obj.add(2, 3)) # This will raise an error because the second add method overwrites the first one print(math_obj.add(2, 3, 4)) # This will work fine and call the second add method
Method Overriding: Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass (parent class). The subclass method with the same name and parameters overrides the behavior of the superclass method
#Example of Method Overriding class Animal: def make_sound(self): return "Generic animal sound." class Dog(Animal): def make_sound(self): return "Woof!" class Cat(Animal): def make_sound(self): return "Meow!" dog = Dog() cat = Cat() print(dog.make_sound()) # Output: Woof! print(cat.make_sound()) # Output: Meow!
Q4.What is Webdriver in selenium?
In Selenium, a WebDriver is a component that provides a programming interface to control and automate web browsers. It allows you to simulate user interactions with a web page, such as clicking buttons, filling forms, and navigating between pages, all programmatically.
Example:
Let’s say you want to automate the process of opening a web page, entering text into a search box and clicking the search button using a WebDriver. In this case, you can use the WebDriver to write the code that instructs the browser to perform these actions.
from selenium import webdriver # Initialize a WebDriver (e.g., Chrome) driver = webdriver.Chrome() # Open a web page driver.get("https://www.example.com") # Find and interact with elements search_box = driver.find_element_by_name("q") search_box.send_keys("Selenium WebDriver") search_button = driver.find_element_by_name("btnK") search_button.click() # Close the browser driver.quit()
Q5.Print the following output using Python.
*
**
***
****
*****
stars = "" # Initialize an empty string named 'stars' for i in range(5): # Loop from 0 to 4 (inclusive) stars += "*" # Add an asterisk to the 'stars' string print(stars) # Print the current content of the 'stars' string
stars = ""
: This line initializes an empty string namedstars
. This string will be used to accumulate asterisks as we iterate through the loop.for i in range(5):
: This line sets up a loop that iterates through numbers from 0 to 4 (inclusive). The loop variablei
takes on each value in this range one by one.stars += "*"
: Inside the loop, an asterisk (*
) is added to thestars
string. This line appends an asterisk to the end of the current content of the string in each iteration.print(stars)
: After adding an asterisk in each iteration, the current content of thestars
string is printed. Since thestars
string grows with each iteration, printing it results in the incremental pattern of asterisks.
Q6.Open the Ajio website, search jeans in the search box and fetch the brand name, price and original price of the first 6 records.
See solution
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Create a WebDriver instance (ensure you have the appropriate WebDriver for your browser) driver = webdriver.Chrome() #maximize the window driver.maximize_window() # Open Ajio website driver.get("https://www.ajio.com/") # Find the search box and enter "jeans" search_box = driver.find_element(By.NAME, "searchVal") search_box.send_keys("jeans") search_box.send_keys(Keys.RETURN) # Wait for search results to load wait = WebDriverWait(driver, 10) wait.until(EC.presence_of_element_located((By.CLASS_NAME, "brand"))) # Find and fetch the details of the first 6 records for i in range(6): try: # Find elements for brand, price and original price brand_element = driver.find_element(By.XPATH, f'(//div[@class="brand"])[{i+1}]') price_element = driver.find_element(By.XPATH, f'(//span[@class="price "])[{i+1}]') original_price_element = driver.find_element(By.XPATH, f'(//span[@class="orginal-price"])[{i+1}]') # Extract text from the elements brand = brand_element.text price = price_element.text original_price = original_price_element.text # Print the details print(f"Record {i+1}:") print(f"Brand: {brand}") print(f"Price: {price}") print(f"Original Price: {original_price}") print("-" * 30) except Exception as e: print(f"Error fetching details for record {i+1}: {e}") # Close the WebDriver driver.quit()
See result
Record 1: Brand: KOTTY Price: ₹700 Original Price: ₹1,999 ------------------------------ Record 2: Brand: DNMX Price: ₹450 Original Price: ₹899 ------------------------------ Record 3: Brand: DNMX Price: ₹450 Original Price: ₹899 ------------------------------ Record 4: Brand: The Indian Garage Co Price: ₹760 Original Price: ₹1,999 ------------------------------ Record 5: Brand: HJ HASASI Price: ₹1,290 Original Price: ₹2,999 ------------------------------ Record 6: Brand: ORCHID BLUES Price: ₹806 Original Price: ₹2,599 ------------------------------