Programming Knowledge Illustration

Tue Jan 07 - Written by: Samridha

Programming Knowledge

Essential programming knowledge for test engineers, including scripting languages like Python and JavaScript, DOM manipulation for frontend validation, and SQL/NoSQL database basics for backend testing.

Programming Knowledge

Scripting Languages

  • Python and JavaScript are widely used for test automation.

Python

Key Features:

  • Data types: int, float, complex, str, bool, list, tuple, dict, set.

  • Variable-length arguments and classes for object-oriented programming.

  • File handling and exception handling for robust scripting.

    Classes for OOP
    def add_numbers(*args):
      return sum(args)
    
    print(add_numbers(1, 2, 3, 4))  # Output: 10
    
    class person:
    def __init__(self, age):
      self.age = age
    
    File Handling
    with open("file_name.txt", r) as file:
        file.readlines()
    
    Exception Handling
    try:
        element = driver.find_element(By.ID, "summit")
    catch Expection as e:
        print("error :"{e})
    
    Selenium
    from selenium import webdriver
    
    driver = webdriver.chrome()
    element = driver.find_element(By.ID, "1234)
    
  • Libraries like:

    • Pandas for data manipulation.
    • Requests for API interactions.

Understanding Frameworks:

  • PyTest vs Unittest:
    • Unittest: A class-based testing framework in Python.
      import unittest
      
      class test_login(unittest.TestCase):
        def setUp(self,):
          pass
        def test_login(self, username, password):
          self.assertEqual()
          pass
        def tearDown(self):
          pass
      
    • PyTest: A simpler, function-based framework.
AspectPyTestUnittest
Ease of UseSimple function-basedUses complex structures
Setup/TeardownFixtures (pytest.fixture)setUp() and tearDown()
AssertionNo special methodsSpecial methods like assertEqual()
Plugin SupportExtensive plugin supportLimited plugin support
SpeedFasterComparatively slower
Test DetectionAutomatically detects testsDetects tests in test*.py files

Web Fundamentals

  • HTML, CSS, and JavaScript for validating frontend issues.

Basic DOM Manipulation and Debugging:

  • DOM (Document Object Model): Represents the content of HTML/XML documents as a hierarchical tree structure.
  • Allows developers to dynamically access, modify, and interact with the content of a webpage.

Key DOM Manipulation Tasks:

  1. Change content dynamically (e.g., updating titles).
  2. Get and set attributes of elements.
  3. Add or remove classes of elements (e.g., active, deactivate, toggle).
  4. Create and append elements to the DOM.
  5. Remove elements from the DOM.
JavaScript Code
  document.getElementById("title").textContent = "Updated Title!";
  const title = document.getElementById("title");
  const buttons = document.getElementsByClassName("btn");
  const firstLink = document.querySelector("a");
  const allLinks = document.querySelectorAll("a");

DOM Manipulation Tools:

  • Inspect elements.
  • console
  • Beakpoints.
  • Event listeners.

Database Basics

  • Both SQL and NoSQL databases are commonly used to store and manage data. They differ in structure and usage.

SQL Databases:

  • Relational databases where data is stored in tables with a fixed schema.
  • Key SQL Commands:
    • CREATE TABLE, INSERT INTO, SELECT, UPDATE, and DELETE.
      # Create table
      CREATE TABLE table_name(
      id INT PRIMARY KEY,
      name VARCHAR(50),
      password VARCHAR(50)
      );
    
      # Insert into table
      INSERT INTO table_name (id, name, password)
      VALUES (1, "sam", "ridha");
    
      # retrive
        SELECT * FROM table_name WHERE id=1;
        
      # Update a row
      UPDATE table_name
      SET name = "SAMRIDHA"
      WHERE id = 1;
    
      # delete a row
      DELETE FROM table_name
      WHERE id=1
    

NoSQL Databases:

  • Non-relational databases designed for flexibility and scalability.
  • Example: MongoDB, DynamoDB.
  • Features:
    • No fixed schema; data is stored as key-value pairs or in JSON-like documents.
    • Ideal for unstructured data or large-scale applications.
db.users.insertone({id: 1, name: "sam", password:"ridha"});
db.users.find({id:1});
db.users.updateOne({id: 1},
$set : {name: "samridha"});