spd
spd

Reputation: 1

Verify background color for date of month

Calendar

I have a calendar that shows each day of attendance. Each date has a different color display (Example: If an employee is present, then it is green. If absent, it should be red. If it's a holiday, it should be gray.) Now, I want to check each day shows the correct color according to the status. I have tried the code below in watir selenium:

require 'color'

def get_current_month_and_year_from_calendar(browser)  
  calendar_title_element = @browser.span(class: 'calendar-title')
  calendar_title = calendar_title_element.text
  month, year = calendar_title.split(' ')
  [month, year.to_i]
end

backward_button = @browser.element(xpath: '/html/body/div/section/div[2]/div/div/div[2]/div[1]/div/div/div/a[1]')

current_month, current_year = get_current_month_and_year_from_calendar(@browser)
target_month = "January"
target_year = 2024

while !(current_month == target_month && current_year == target_year)
  backward_button.click
  sleep 1
  current_month, current_year = get_current_month_and_year_from_calendar(@browser)
end

def check_attendance_for_month(browser)
  month_calendar = @browser.elements(xpath: "//table[@class='simple-calendar']//td[contains(@class,'current-month')]")
  
  month_calendar.each do |day|
    color = get_color_for_day(day)
    if day_is_present?(color)
      puts "Day #{day.text} is present"
    elsif day_is_Work_From_Home?(color)
      puts "Day #{day.text} is Work From Home"
    elsif day_is_CompOff_Present?(color)
      puts "Day #{day.text} is CompOff and Present"
    else
      puts "Day #{day.text} has an unknown color"
    end
  end
end

def get_color_for_day(day_element)
  color_style = day_element.style('background-color')
  parse_color(color_style)
end

def parse_color(color_style)
  rgb_match = color_style.match(/\Argba?\((\d+),\s*(\d+),\s*(\d+)/)
  if rgb_match
    red = rgb_match[1].to_i
    green = rgb_match[2].to_i
    blue = rgb_match[3].to_i
    return [red, green, blue]
  end
  nil
end

def day_is_present?(color)
  present_color = [0, 128, 0]  # Green
  compare_colors(color, present_color)
end

def day_is_CompOff_Present?(color)
  compOff_present_color = [255, 165, 0]  # Orange
  compare_colors(color, compOff_present_color)
end

def day_is_Work_From_Home?(color)
  work_from_home_color = [0, 0, 255]  # Blue
  compare_colors(color, work_from_home_color)
end

def compare_colors(color, target_color)
  color && color == target_color
end

check_attendance_for_month(@browser)

end

This code moves the calendar from the current month to January 2024, but it doesn't give the color for the day, which I require.

Upvotes: -1

Views: 96

Answers (1)

JeffC
JeffC

Reputation: 25724

One problem I see is that get_color_for_day() isn't returning the color. Change it to

def get_color_for_day(day_element)
  color_style = day_element.style('background-color')
  return parse_color(color_style)
end

and at least that bug will be fixed.

Upvotes: 0

Related Questions