def test_first_second_separation_invalid(self):
     # Tests the case where only 2 out of 3 digits are read by OCR. Should return 0.
     captcha_solver = CaptchaSolver(None)
     test_img = cv2.imread(os.path.join(os.getcwd(), 'test_ocr_invalid.png'))
     assert captcha_solver.solve_captcha(test_img) == 0
     assert captcha_solver.first_number is None
     assert captcha_solver.second_number is None
 def test_first_second_separation(self):
     # Tests correct separation of first_number (first 2 digits) and second_number (last digit)
     captcha_solver = CaptchaSolver(None)
     test_img = cv2.imread(os.path.join(os.getcwd(), 'test_ocr_valid.png'))
     assert captcha_solver.solve_captcha(test_img) == 15
     assert captcha_solver.first_number == 12
     assert captcha_solver.second_number == 3
 def test_ocr(self):
     # Do OCR on easy image to ensure it's working
     captcha_solver = CaptchaSolver(None)
     test_img1 = cv2.imread(os.path.join(os.getcwd(), 'test_ocr_valid.png'))
     test_img2 = cv2.imread(os.path.join(os.getcwd(), 'test_ocr_invalid.png'))
     captcha_text1 = captcha_solver.read_captcha(test_img1)
     captcha_text2 = captcha_solver.read_captcha(test_img2)
     assert captcha_text1 == '123'
     assert captcha_text2 == '12'
    def test_first_second_separation_invalid(self, tmpdir, testdatadir):
        # Tests the case where only 2 out of 3 digits are read by OCR. Should return 0.
        td = tmpdir.mkdir('captcha')
        captcha_solver = CaptchaSolver(None, outdir=td)

        test_img = cv2.imread(testdatadir.join('test_ocr_invalid.png').strpath)

        assert captcha_solver.solve_captcha(test_img) == 0
        assert captcha_solver.first_number is None
        assert captcha_solver.second_number is None
    def test_first_second_separation(self, tmpdir, testdatadir):
        # Tests correct separation of first_number (first 2 digits) and second_number (last digit)
        td = tmpdir.mkdir('captcha')
        captcha_solver = CaptchaSolver(None, outdir=td)

        test_img = cv2.imread(testdatadir.join('test_ocr_valid.png').strpath)

        assert captcha_solver.solve_captcha(test_img) == 15
        assert captcha_solver.first_number == 12
        assert captcha_solver.second_number == 3
    def test_ocr(self, tmpdir, testdatadir):
        # Do OCR on easy image to ensure it's working
        td = tmpdir.mkdir('captcha')
        captcha_solver = CaptchaSolver(None, outdir=td)

        test_img1 = cv2.imread(testdatadir.join('test_ocr_valid.png').strpath)
        test_img2 = cv2.imread(
            testdatadir.join('test_ocr_invalid.png').strpath)
        captcha_text1 = captcha_solver.read_captcha(test_img1)
        captcha_text2 = captcha_solver.read_captcha(test_img2)

        assert captcha_text1 == '123'
        assert captcha_text2 == '12'
    def test_captcha_fail_save(self):
        captcha_solver = CaptchaSolver(None)
        test_img = cv2.imread(os.path.join(os.getcwd(), 'test_ocr_valid.png'))
        captcha_solver.solve_captcha(test_img)
        captcha_solver.notify_last_captcha_fail()
        assert os.path.exists(os.path.join(os.getcwd(), 'captcha', 'incorrect', 'captcha1.png'))
        captcha_solver.notify_last_captcha_fail()
        assert os.path.exists(os.path.join(os.getcwd(), 'captcha', 'incorrect', 'captcha2.png'))

        os.remove(os.path.join(os.getcwd(), 'captcha', 'incorrect', 'captcha1.png'))
        os.remove(os.path.join(os.getcwd(), 'captcha', 'incorrect', 'captcha2.png'))
    def test_captcha_fail_save(self, tmpdir, testdatadir):
        td = tmpdir.mkdir('captcha')
        captcha_solver = CaptchaSolver(None, outdir=td)
        test_img = cv2.imread(testdatadir.join('test_ocr_valid.png').strpath)

        captcha_solver.solve_captcha(test_img)
        captcha_solver.notify_last_captcha_fail()
        assert os.path.exists(td.join('incorrect', 'captcha1.png'))

        captcha_solver.notify_last_captcha_fail()
        assert os.path.exists(td.join('incorrect', 'captcha2.png'))
 def test_folder_creation(self):
     # Note, this only removes the captcha folder in the tests directory.
     os.rmdir(os.path.join(os.getcwd(), 'captcha', 'incorrect'))
     os.rmdir(os.path.join(os.getcwd(), 'captcha', 'correct'))
     captcha_solver = CaptchaSolver(None)
     assert os.path.exists(os.path.join(os.getcwd(), 'captcha', 'incorrect'))
     assert os.path.exists(os.path.join(os.getcwd(), 'captcha', 'correct'))
     # Cleanup folders
     os.rmdir(os.path.join(os.getcwd(), 'captcha', 'incorrect'))
     os.rmdir(os.path.join(os.getcwd(), 'captcha', 'correct'))
    'collect-pii': False,
    'connect-thresh': 10,
    'output': 'bay-county-scraped.csv',
    'save-attachments': 'none',
    'solve-captchas': False,
    'verbose': False
}

output_attachments = os.path.join(os.getcwd(), 'attachments')
output_file = os.path.join(os.getcwd(), settings['output'])

ffx_profile = webdriver.FirefoxOptions()
# Automatically dismiss unexpected alerts.
ffx_profile.set_capability('unexpectedAlertBehaviour', 'dismiss')
driver = webdriver.Firefox(options=ffx_profile)
captcha_solver = CaptchaSolver(driver)


def main():
    # Parse Arguments
    args = sys.argv[1:]
    short_args = 'p:s:c:y:e:t:pc:o:a:uv'
    long_args = ['portal-base=', 'state=', 'county', 'start-year=', 'end-year=', 'missing-thresh=', 'collect-pii',
                 'connect-thresh=', 'output=', 'save-attachments=','solve-captchas', 'verbose']

    try:
        args, vals = getopt.getopt(args, short_args, long_args)
        for arg, val in args:
            if arg in ('-p', '--portal-base'):
                settings['portal-base'] = val
            elif arg in ('-s', '--state'):
    def test_folder_creation(self, tmpdir):
        td = tmpdir.mkdir('captcha')
        captcha_solver = CaptchaSolver(None, outdir=td)

        assert os.path.exists(td.join('incorrect'))
        assert os.path.exists(td.join('correct'))