我有一个日历,显示每天的出勤情况。每个日期都有不同的颜色显示(例如:如果有员工在场,则为绿色。如果缺席,则应为红色。如果是假期,则应为灰色。)现在,我想检查每天显示的根据状态正确颜色。我已经在 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
此代码将日历从当前月份移动到 2024 年 1 月,但它没有给出我需要的当天的颜色。
我看到的一个问题是
get_color_for_day()
没有返回颜色。改为
def get_color_for_day(day_element)
color_style = day_element.style('background-color')
return parse_color(color_style)
end
至少这个错误会被修复。