Wednesday, March 22, 2023

Python ispalindrome function

 def is_palindrome(input_string):

    new_string = ""
    reverse_string = ""

    for letter in input_string:

       if letter != " ":

            new_string = new_string + letter
            reverse_string = letter + reverse_string
 
    if new_string.replace(" """).lower() == reverse_string.replace(" """).lower():

        return True

    return False


****************************************************************
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

Thursday, March 16, 2023

Python : Calculate_storage program

Question : If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you prepare a function which calculates the total number of bytes needed to…

def calculate_storage(filesize):
    block_size = 4096
    full_blocks = filesize//4096
    partial_block_remainder = filesize%4096
    if partial_block_remainder > 0:
        return (full_blocks+1)*4096
    return full_blocks*4096

Output:
print(calculate_storage(1)) # Should be 4096 print(calculate_storage(4096)) # Should be 4096 print(calculate_storage(4097)) # Should be 8192 print(calculate_storage(6000)) # Should be 8192

Wednesday, June 15, 2016

Stale Element Reference exception in Selenium Webdriver



There may be cases when you get exception “Element is no longer attached to the DOM”. This is due to State Element Execption.

I will show you the problem here and then it’s solution that how we can overcome with this issue.

Just take an example of Cleartrip.com where in round trip flight search, you will have to select

DepartDate and ReturnDate from date picker. Below is the code to automate same using webdriver in TestNg Framework.


Here I am giving you the complete set of code so that you run it and see what happening. 



import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
 
public class roundTripSearch
{
 WebDriver driver= new FirefoxDriver();
                          
       @Test
       public void tc01_RoundTripSearchResult() throws InterruptedException
       {
             
              driver.get("http://www.cleartrip.com/");
              if(driver.findElement(By.id("RoundTrip")).isSelected() == true){}
              else driver.findElement(By.id("RoundTrip")).click();
driver.findElement(By.id("FromTag")).sendKeys("New Delhi, IN - Indira Gandhi Airport (DEL)");
driver.findElement(By.id("ToTag")).sendKeys("Mumbai, IN - Chatrapati Shivaji Airport (BOM)");
             
              driver.findElement(By.id("DepartDate")).click();
              List col1= driver.findElements(By.tagName("td"));
                      for (WebElement box: col1)
                      {           
                           if(box.getText().equals("24"))
                                  {
                                  box.findElement(By.linkText("24")).click();
                                  }
                       }
                      
              driver.findElement(By.id("ReturnDate")).click();
              List col2= driver.findElements(By.tagName("td"));
                     for (WebElement box: col2)
                      {           
                           if(box.getText().equals("27"))
                                  {
                                  box.findElement(By.linkText("27")).click();
                                  }
                                               
                     }
                    
               driver.findElement(By.id("Adults")).sendKeys("2");
               driver.findElement(By.id("Childrens")).sendKeys("1");
               driver.findElement(By.id("Infants")).sendKeys("0");
               driver.findElement(By.id("SearchBtn")).click();
                                    
       }
      
       @AfterTest
              public void closebrowser()
       {
              driver.close();
       }
             
}


If you run this, you will get the error with exception:

FAILED: tc01_RoundTripSearchResult
org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
Command duration or timeout: 9 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'

 
This is due to use of java script. When we select first date, it automatically open second calendar and the element is lost in way. Here I fixed this using break statement.  See the code..it works fine.



import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;


public class roundTripSearch
{

WebDriver driver= new FirefoxDriver();
             
                    
       @Test
       public void tc01_RoundTripSearchResult() throws InterruptedException
       {
             
              driver.get("http://www.cleartrip.com/");
              if(driver.findElement(By.id("RoundTrip")).isSelected() == true){}
              else driver.findElement(By.id("RoundTrip")).click();
              driver.findElement(By.id("FromTag")).sendKeys("New Delhi, IN - Indira Gandhi Airport (DEL)");
              driver.findElement(By.id("ToTag")).sendKeys("Mumbai, IN - Chatrapati Shivaji Airport (BOM)");
             
              driver.findElement(By.id("DepartDate")).click();
              List col1= driver.findElements(By.tagName("td"));
                      for (WebElement box: col1)
                      {           
                           if(box.getText().equals("24"))
                                  {
                                  box.findElement(By.linkText("24")).click();
                                  break;
                                  }
                       }
                      
              driver.findElement(By.id("ReturnDate")).click();
              List col2= driver.findElements(By.tagName("td"));
                     for (WebElement box: col2)
                      {           
                           if(box.getText().equals("27"))
                                  {
                                  box.findElement(By.linkText("27")).click();
                                  break;
                                  }
                                               
                     }
                    
               driver.findElement(By.id("Adults")).sendKeys("2");
               driver.findElement(By.id("Childrens")).sendKeys("1");
               driver.findElement(By.id("Infants")).sendKeys("0");
               driver.findElement(By.id("SearchBtn")).click();
                                    
       }
      
       @AfterTest
              public void closebrowser()
       {
              driver.close();
       }
             
}


Enjoy.