seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
5.34k stars 980 forks source link

How does MySQL log in using a non-default port? #503

Closed cw010 closed 4 years ago

cw010 commented 4 years ago

APP_CREDS = {

Apps.TESTCASE_REPOSITORY: {
    TEST: (
        settings.DB_HOST,
        settings.DB_USERNAME,
        settings.DB_PASSWORD,
        settings.DB_SCHEMA)
},
cw010 commented 4 years ago

When running, I used --settings = custom_settings.py and modified the default configuration file, but the database still says that I cannot connect

mdmintz commented 4 years ago

Hi @tcwaily , Looks like right now the following is the connect command:

self.conn = pymysql.connect(host=db_server,
                            user=db_user,
                            passwd=db_pass,
                            db=db_schema)

It's going to use the default port, but I can change that in the next release so that you can specify a custom port in the settings file. I'll probably make another release sometime in the next two days.

mdmintz commented 4 years ago

I found examples here: https://www.programcreek.com/python/example/53260/pymysql.connect and it looks like 3306 is the default port when not specified. I can use that to expand what I have so far.

cw010 commented 4 years ago

I found examples here: https://www.programcreek.com/python/example/53260/pymysql.connect and it looks like 3306 is the default port when not specified. I can use that to expand what I have so far.

Hi @tcwaily , Looks like right now the following is the connect command:

self.conn = pymysql.connect(host=db_server,
                            user=db_user,
                            passwd=db_pass,
                            db=db_schema)

It's going to use the default port, but I can change that in the next release so that you can specify a custom port in the settings file. I'll probably make another release sometime in the next two days.

I recreated the settings file and added --settings = custom_settings.py at runtime, but I found that it didn't work. Have you tested this?

cw010 commented 4 years ago

I found examples here: https://www.programcreek.com/python/example/53260/pymysql.connect and it looks like 3306 is the default port when not specified. I can use that to expand what I have so far.

I know this, but the MySQL started by docker I use is not the default port, so I can't connect now。

mdmintz commented 4 years ago

@tcwaily I would have to change that. The custom_settings.py file only lets you make changes to settings that already have a row in the original settings.py file. Right now the MySQL port used is the default one, but I'm going to change that so that you can specify a port.

cw010 commented 4 years ago

@tcwaily I would have to change that. The custom_settings.py file only lets you make changes to settings that already have a row in the original settings.py file. Right now the MySQL port used is the default one, but I'm going to change that so that you can specify a port.

I mean I now have a database with a default port 3306, but I used custom_settings.py to set a new host address, but it still cannot connect

cw010 commented 4 years ago

@tcwaily I would have to change that. The custom_settings.py file only lets you make changes to settings that already have a row in the original settings.py file. Right now the MySQL port used is the default one, but I'm going to change that so that you can specify a port.

Add "--with-db_reporting" to save test data to a MySQL DB during test runs

DB_HOST = "localhost" DB_USERNAME = "root" DB_PASSWORD = "123456" DB_SCHEMA = "test"

Unable to connect to Database after 3 retries.

The default port of this database is already 3306, but it cannot be connected, so I suspect --settings = custom_settings.py this setting has no effect

mdmintz commented 4 years ago

@tcwaily Which version of SeleniumBase are you using? I fixed the usage of the custom_settings.py file in https://github.com/seleniumbase/SeleniumBase/releases/tag/v1.32.16 (last October).

cw010 commented 4 years ago

@tcwaily Which version of SeleniumBase are you using? I fixed the usage of the custom_settings.py file in https://github.com/seleniumbase/SeleniumBase/releases/tag/v1.32.16 (last October).

pytest>=5.3.5 selenium==3.141.0 pytest-html==2.0.1 seleniumbase==1.35.4 requests==2.22.0 BeautifulSoup4==4.8.2 pyyaml==5.3 tkmacosx flask-executor==0.9.3 flask==1.1.1

mdmintz commented 4 years ago

I'll investigate this issue.

cw010 commented 4 years ago

I'll investigate this issue.

Ok, look forward to your findings。

mdmintz commented 4 years ago

In the meantime, https://travis-ci.org/seleniumbase/SeleniumBase/jobs/651853851 has a working example of MySQL. See the results table at the bottom.

cw010 commented 4 years ago

In the meantime, https://travis-ci.org/seleniumbase/SeleniumBase/jobs/651853851 has a working example of MySQL. See the results table at the bottom.

Also use --with-db_reporting --settings=custom_settings.py Is there a problem?

mdmintz commented 4 years ago

I'll try several examples and post findings here at the end of the day. I have to get to work.

mdmintz commented 4 years ago

@tcw0nw Looks like it broke here: https://github.com/seleniumbase/SeleniumBase/pull/459 I’ll try to get a fix pushed sometime in the next 13 hours.

mdmintz commented 4 years ago

Ignore the last comment. I was mixing up the desired capabilities parser with the custom settings parser. I'm looking at it now.

mdmintz commented 4 years ago

@tcw0nw Should be fixed by https://github.com/seleniumbase/SeleniumBase/releases/tag/v1.35.5

mdmintz commented 4 years ago

I just added a bit more with https://github.com/seleniumbase/SeleniumBase/releases/tag/v1.35.6

cw010 commented 4 years ago

I just added a bit more with https://github.com/seleniumbase/SeleniumBase/releases/tag/v1.35.6

pytest testset/test2.py --settings=custom_settings.py --with-db_reporting

DB_HOST = "localhost" DB_USERNAME = "root" DB_PORT = "3306" DB_PASSWORD = "123456" DB_SCHEMA = "test_db"

I get the following error after running: self = <seleniumbase.core.mysql.DatabaseManager object at 0x10d5ae710>, database_env = 'test', conf_creds = None

def __init__(self, database_env='test', conf_creds=None):
    """
    Create a connection to the MySQL DB.
    """
    import pymysql
    db_server = settings.DB_HOST
    db_port = settings.DB_PORT
    db_user = settings.DB_USERNAME
    db_pass = settings.DB_PASSWORD
    db_schema = settings.DB_SCHEMA
    if hasattr(sb_config, "settings_file") and sb_config.settings_file:
        override = settings_parser.set_settings(sb_config.settings_file)
        if "DB_HOST" in override.keys():
            db_server = override['DB_HOST']
        if "DB_PORT" in override.keys():
            db_port = override['DB_PORT']
        if "DB_USERNAME" in override.keys():
            db_user = override['DB_USERNAME']
        if "DB_PASSWORD" in override.keys():
            db_pass = override['DB_PASSWORD']
        if "DB_SCHEMA" in override.keys():
            db_schema = override['DB_SCHEMA']
    retry_count = 3
    backoff = 1.2  # Time to wait (in seconds) between retries.
    count = 0
    while count < retry_count:
        try:
            self.conn = pymysql.connect(host=db_server,
                                        port=db_port,
                                        user=db_user,
                                        passwd=db_pass,
                                        db=db_schema)
            self.conn.autocommit(True)
            self.cursor = self.conn.cursor()
            return
        except Exception:
            time.sleep(backoff)
            count = count + 1
    if retry_count == 3:
      raise Exception("Unable to connect to Database after 3 retries.")

E Exception: Unable to connect to Database after 3 retries.

I upgraded 1.35.6 while using --settings=custom_settings.py --with-db_reporting unable to connect

cw010 commented 4 years ago

Verify again and pass. thank!